javascript Underscore.js - 将键/值对数组映射到一个对象 - 一个班轮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17802140/
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
Underscore.js - Map Array of key/value pairs to an Object - One liner
提问by parliament
I've been going through the underscore docsbut I can't seem to find a method (or nested method call) to do the following transformation:
我一直在浏览下划线文档,但似乎找不到方法(或嵌套方法调用)来执行以下转换:
Let's say I have the following Javascript array:
假设我有以下 Javascript 数组:
[{ "name" : "sEcho", "value" : 1},{ "name" : "iColumns", "value" : 12}, ... ]
And I need to transform it into the following object:
我需要将其转换为以下对象:
{
sEcho: 1,
iColumns: 12,
...
}
I'm using underscore.js for a reason so it mustbe a one liner.
我使用 underscore.js 是有原因的,所以它必须是单行的。
回答by John Dvorak
Variation on Sza's answer, using the "array of pairs" signature of _.object
:
Sza 的答案的变体,使用的“对数组”签名_.object
:
_.object(_.map(data, function(x){return [x.name, x.value]}))
回答by Brad Pitcher
Since nobody has posted this as an answer, I'm posting it because I think it is better than Jan's answer. It's shorter, and cleaner without the inline function.
由于没有人将此作为答案发布,因此我发布它是因为我认为它比Jan 的答案更好。没有内联函数,它更短、更干净。
_.object(_.pluck(data, 'name'), _.pluck(data, 'value'));
回答by Esailija
This should do it:
这应该这样做:
_.reduce(array, function(o, v){
o[v.name] = v.value;
return o;
}, {});
As a one-liner (you are kidding me, right?):
作为单线(你在开玩笑吧?):
_.reduce(array,function(a,b){a[b.name]=b.value;return a},{});
回答by Aadit M Shah
Let's say you have the following JavaScript array:
假设您有以下 JavaScript 数组:
var list = [
{
name: "sEcho",
value: 1
},
{
name: "iColumns",
value: 12
},
...
];
You can convert it into the format you want as follows:
您可以将其转换为您想要的格式,如下所示:
var table = _.reduce(list, function (table, item) {
table[item.name] = item.value;
return table;
}, {});
It's not a one liner, but I don't think you literally meant a one liner. Did you?
这不是单衬,但我不认为你的字面意思是单衬。你是否?
Here's a one liner if you really meant a one liner:
如果您的意思是单衬,这是单衬:
var t = _.reduce(list, function (t, i) { return t[i.name] = i.value, t; }, {});
Yes, others have provided the same answer. However that's only because the answer to your question is so simple.
是的,其他人也提供了相同的答案。然而,这只是因为你的问题的答案很简单。
回答by zs2020
var names = _.pluck(data, 'name');
var values = _.pluck(data, 'value');
var result = _.object(_.zip(names, values));
console.log(result);
回答by Amadan
var a = [{ "name" : "sEcho", "value" : 1},{ "name" : "iColumns", "value" : 12} ];
var o = {}; _.each(a, function(e) { o[e.name] = e.value; });
console.log(o);
// Object {sEcho: 1, iColumns: 12}
回答by user2240431
perfect place to use reduce I think:
我认为使用 reduce 的完美场所:
_.reduce(ary, function(memo, obj){ memo[obj["name"]] = obj["value"]; return memo }, {});
_.reduce(ary, function(memo, obj){ memo[obj["name"]] = obj["value"]; return memo }, {});
回答by bentael
var arr = [{ "name" : "sEcho", "value" : 1},{ "name" : "iColumns", "value" : 12}]
ES6
ES6
_.mapObject( _.indexBy(arr, 'name'), (v) => v.value )
ES5
ES5
_.mapObject( _.indexBy(arr, 'name'), function (v) { return v.value; } )
This iterates twice though
这虽然迭代了两次