I need to generate a couple of objects from lists in Javascript. In Python, I’d write this:
{key_maker(x): val_maker(x) for x in a_list}
Another way to ask is does there exist something like jQuery.map() which aggregates objects? Here’s my guess (doesn’t work):
var result = {}
$.map(a_list, function(x) {
$.extend(result, {key_maker(x): val_maker(x)})
})
Assuming
a_listis an Array, the closest would probably be to use.reduce().Array comprehensions are likely coming in a future version of JavaScript.
You can patch non ES5 compliant implementations with the compatibility patch from MDN.
If
a_listis not an Array, but a plain object, you can useObject.keys()to perform the same operation.