如何将此对象数组映射到新对象的数组,其中对象的ID是键并且其值是对象?将下划线的对象数组映射到ID为key的新对象
var players = [{id: 2, score: 5}, {id: 3, score: 10}]
var mapped = _.map(players, function(x) { return {x.id: x}})
// desired output = [{2: {id: 2, score: 4}, {3: {id: 3, score: 10}} ]
我得到一个语法错误试图在你的JS小提琴你不包括underscore上述Uncaught SyntaxError: Unexpected token .(…)
我想弄清楚一个更好的方法,我使用这种方法结束。 –