Underscore map array of object to new object with id as key


This question already has an answer here:


First of all, in your JS Fiddle you were not including underscore, but this will work:

var mapped = _.map(players, function(player) {
  var playerObj = {};
  playerObj[player.id] = player;
  return playerObj;

If you're happy using ES6 functionality then you could use an arrow function which makes use of a computed property name:

var mapped = _.map(players, player => ({ [player.id] : player}));

Need Your Help

Passing the name of a table cell to detailed view

iphone ios ios4 sdk

How would I be able to pass the name of a table cell that is clicked in a Master View Controller to a Detailed View Controller. I would like it to be used later in my detailed view right now though I

insert the value automatically in input text when another value is given to another input text

php html codeigniter

In form, I want to insert the value of amount(text input) when value of rate(another text input) . The value which will be inserted in amount is the multiplication of rate and quantity. The value of