javascript Javascript下划线数组到对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22427560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:08:43  来源:igfitidea点击:

Javascript underscore array to object

javascriptarraysobjectunderscore.js

提问by TheBigC

Is there an easy/clean way using Underscore to turn this

有没有一种简单/干净的方法使用 Underscore 来改变这个

[ { id: 'medium', votes: 7 },
  { id: 'low',    votes: 9 },
  { id: 'high',   votes: 5 } ]

Into

进入

 { 'low'    : 9,
   'medium' : 7,
   'high'   : 5 }

回答by kroolk

You might consider _.indexBy(...)

你可能会考虑 _.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/

这是一个小提琴:https: //jsfiddle.net/4vyLtcrf/3/

Update:In Lodash 4.0.1 the method _.indexBy has been renamed to _.keyBy

更新:在 Lodash 4.0.1 中,方法 _.indexBy 已重命名为 _.keyBy

回答by thefourtheye

var data = [ { id: 'medium', votes: 7 },
  { id: 'low',    votes: 9 },
  { id: 'high',   votes: 5 } ];

You can do this with _.map, _.valuesand _.object, like this

你可以用_.map,_.values_.object,像这样

console.log(_.object(_.map(data, _.values)));
# { medium: 7, low: 9, high: 5 }

Explanation

解释

We use the mapfunction to apply the valuesfunction (which gets all the values of a given object) over all the elements of data, which would give

我们使用该map函数将values函数(它获取给定对象的所有值)应用于 的所有元素data,这将给出

# [ [ 'medium', 7 ], [ 'low', 9 ], [ 'high', 5 ] ]

Then we use objectfunction to transform this into an object.

然后我们使用object函数将其转换为对象。

回答by Krasimir

Here is with vanilla js:

这是香草js:

var result = {};
[ { id: 'medium', votes: 7 },
  { id: 'low',    votes: 9 },
  { id: 'high',   votes: 5 } ].forEach(function(obj) {
    result[obj.id] = obj.votes;
});
console.log(result);

回答by nguyên

simpler with me: each:

对我来说更简单each::

   var t = {};
   _.each(x, function(e){
     t[e.id] = e.votes;
   });
//--> {medium: 7, low: 9, high: 5}

回答by Oscar Eduardo

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!

最强大的下划线方法是reduce。你几乎可以用它做任何事情。更大的好处是您只迭代集合一次!

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
}

回答by MarkoCen

I know this is an old post, but you could use _.reduceto do the transform in a clean way

我知道这是一篇旧帖子,但您可以使用_.reduce干净的方式进行转换

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);

回答by Jo-Go

You can do this with native JS Array.reduce

你可以用原生 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

有关更多详细信息,请查看https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

回答by udnisap

Use indexBy

使用 indexBy

_.indexBy([ 
   { id: 'medium', votes: 7 },
   { id: 'low',    votes: 9 },
   { id: 'high',   votes: 5 } 
 ], 'id');