Hello I have object like this:
var obj = { banana: 1425, orange: 1683}
and I need array of objects created on the basis of items. for example:
[{name: banana, value: 1425}, {name: orange, value: 1683}]
I did it in this way :
var fruits = [];
_.each(obj, function(value, name){
fruits.push({
name: name,
value: value
});
});
maybe you know an easier way?
You can use underscore's _.map on an object :
var obj = { banana: 1425, orange: 1683};
var fruits = _.map(obj, function(value, key){
return { name : key, value : value };
});
http://underscorejs.org/#map
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With