Is there an easy/clean way using Underscore to turn this
[ { id: 'medium', votes: 7 },
{ id: 'low', votes: 9 },
{ id: 'high', votes: 5 } ]
Into
{ 'low' : 9,
'medium' : 7,
'high' : 5 }
You might consider _.indexBy(...)
var data = [{
id: 1,
name: 'Jon Doe',
birthdate: '1/1/1991',
height: '5 11'
}, {
id: 2,
name: 'Jane Smith',
birthdate: '1/1/1981',
height: '5 6'
}, {
id: 3,
name: 'Rockin Joe',
birthdate: '4/4/1994',
height: '6 1'
}, {
id: 4,
name: 'Jane Blane',
birthdate: '1/1/1971',
height: '5 9'
}, ];
var transformed = _.indexBy(data, 'id');
Here is a fiddle: https://jsfiddle.net/4vyLtcrf/3/
Update: In Lodash 4.0.1 the method _.indexBy has been renamed to _.keyBy
id property will still be present in the resulting objects, but that should be okay in most cases (and easy to post-exclude in others)._.groupBy()var data = [ { id: 'medium', votes: 7 },
{ id: 'low', votes: 9 },
{ id: 'high', votes: 5 } ];
You can do this with _.map, _.values and _.object, like this
console.log(_.object(_.map(data, _.values)));
# { medium: 7, low: 9, high: 5 }
Explanation
We use the map function to apply the values function (which gets all the values of a given object) over all the elements of data, which would give
# [ [ 'medium', 7 ], [ 'low', 9 ], [ 'high', 5 ] ]
Then we use object function to transform this into an object.
data. There was a newline. I just removed it now.The most powerfull method of underscore is reduce. You can do almost anything with it. And the bigger benefit is you iterate over the collection only ONCE!
var array = [
{ id: 'medium', votes: 7 },
{ id: 'low', votes: 9 },
{ id: 'high', votes: 5 }
];
var object = _.reduce(array, function(_object, item) {
_object[item.id] = item.votes;
return _object;
}, {});
After it runs, object will be:
{
medium:7,
low:9,
high:5
}
I know this is an old post, but you could use _.reduce to do the transform in a clean way
var data = [
{ id: 'medium', votes: 7 },
{ id: 'low', votes: 9 },
{ id: 'high', votes: 5 }
]
var output = _.reduce(data, function(memo, entry) {
memo[entry.id] = entry.votes;
return memo;
}, {});
console.log(output);
You can do this with native JS Array.reduce
const myArray = [ { id: 'medium', votes: 7 },
{ id: 'low', votes: 9 },
{ id: 'high', votes: 5 } ]
const myObject = myArray.reduce((obj, item)=>{
o[item.id] = item.votes
return o
}, {})
for more details, take a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce