var levelsByNumber = {};
for (var levelName in levels) {
var levelNumber = levels[levelName];
levelsByNumber[levelNumber] = levelName;
}
Then you could write:
Code:
console.log(levelsByNumber[level]+' : '+msg);
Though, whether you ultimately use an index or a loop, unless there's more code we haven't seen, I'd say that representing levels using a number costs more than it saves. You'd be better off just passing a string.
Both methods above will work, although I'd hate to run a for loop every time to get the property name. I ended up going Jeff's route in any case. I figured there'd be an easier way...
I've switched careers...
I'm NO LONGER a scientist,
but now a web developer...
awesome.
Bookmarks