Javascript underscore array to object
问题 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 } 回答1: 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