Javascript Underscore.js:使用在对象中找到的键从对象列表中创建映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10416424/
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: create a map out of list of objects using a key found in the object
提问by Chantz
I am using the excellent Underscore.jslibrary. I have a specific task which I can do fine using JavaScript or jQuery but was wondering if there was some sort of abstraction avaialable in Underscore that I was missing out on.
我正在使用优秀的Underscore.js库。我有一个特定的任务,我可以使用 JavaScript 或 jQuery 完成它,但想知道 Underscore 中是否存在某种我错过的抽象。
Essentially I have an object like so -
基本上我有一个这样的对象 -
var some_object_array = [{id: "a", val: 55}, {id: "b", val: 1}, {id: "c", val: 45}];
I want to convert this into -
我想把它转换成 -
var some_map = {"a": {id: "a", val: 55}, "b": {id: "b", val: 1}, "c": {id: "c", val: 45}};
I know that I can use _.groupBy(some_object_array, "id")
. But this returns a map like so -
我知道我可以使用_.groupBy(some_object_array, "id")
. 但这会返回一张像这样的地图 -
var some_grouped_map = {"a": [{id: "a", val: 55}], "b": [{id: "b", val: 1}], "c": [{id: "c", val: 45}]};
Note that this does what it is advertised to do. But I was hoping to get some_map
without iterating over the objects myself.
请注意,这是做广告宣传的。但我希望自己some_map
不迭代对象。
Any help appreciated.
任何帮助表示赞赏。
回答by cletus
For what it's worth, since underscore.js you can now use _.object()
对于它的价值,因为你现在可以使用 underscore.js _.object()
var some_map = _.object(_.map(some_object_array, function(item) {
return [item.id, item]
}));
回答by user3699395
for your case, you should use the indexBy
function:
对于您的情况,您应该使用该indexBy
功能:
var some_object_array = [{id: "a", val: 55}, {id: "b", val: 1}, {id: "c", val: 45}];
var some_grouped_map = _.indexBy(some_object_array, 'id');
回答by Otto Allmendinger
There is also this method
还有这个方法
_.reduce(data, function (o, item) { o[item.key] = item.value; return o }, {})
Which is one statement with two statements in the inner function.
这是一个内部函数中有两个语句的语句。
回答by Marwane K.A.
I don't think there's something closer than groupBy for your needs. Even if there was, it wouldn't do better than a simple:
我认为没有什么比 groupBy 更能满足您的需求了。即使有,它也不会比简单的更好:
var some_map = {};
_.each(some_object_array, function(val) {
some_map[val.id] = val;
});
回答by AA.
I this case you don't need to iterate the array. Not map
, not reduce
, not transform
. All you need is the old pluck
在这种情况下,您不需要迭代数组。不map
,不reduce
,不transform
。你只需要旧的pluck
_.object(_.pluck(some_object_array, 'id'), some_object_array);
回答by ryan
for ES6, it's
对于 ES6,它是
var some_map = _.chain(some_object_array)
.map(item => [item.id, item])
.object()
.value()
回答by davidsonsns
I solved this way, using lodash:
我是这样解决的,使用 lodash:
const ar = [
{ key: '1', val: 'a' },
{ key: '2', val: 'b' },
{ key: '3', val: 'c' }
]
const obj = _.keyBy({ ...ar }, 'key')
console.log(obj)
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>