javascript 如何将对象数组转换为 Lodash 中的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30221286/
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
How to convert an array of objects to an object in Lodash?
提问by Stefan
I have this:
我有这个:
[ { list:
[ [Object],
[Object] ] },
{ head:
[ [Object],
[Object] ] }
]
And want to turn it into this:
并想把它变成这样:
{ list:
[ [Object],
[Object] ],
head: [ [Object],
[Object] ]
}
So an array of objects into an object. It would be great to achieve this with lodash.
于是一个对象数组变成了一个对象。用 lodash 实现这一点会很棒。
回答by djaszczurowski
_.reduce(array, function(memo, current) { return _.assign(memo, current) }, {})
回答by rcsole
I think an even shorter solution would be:
我认为一个更短的解决方案是:
Object.assign({}, ...array)
I know you asked for lodash, but it doesn't seem like you even need this way. Unless you want to use _.extend
.
我知道您要求使用 lodash,但似乎您甚至不需要这种方式。除非你想使用_.extend
.
回答by Johannes Fahrenkrug
To build on @rcsole's great answer, this works well:
为了建立在@rcsole 的伟大答案之上,这很有效:
states = [{
state: "NY",
name: "New York",
}, {
state: "AZ",
name: "Arizona",
}]
statesObj = Object.assign({}, ...states.map(state => {
return { [state.state]: state.name }
}))
Result:
结果:
{
AZ: "Arizona",
NY: "New York",
}
What's going on here?
这里发生了什么?
Let's break this up into multiple pieces:
让我们把它分解成多个部分:
// Step 1: Transform from [{state: "Foo", name: "Bar"}, ...] to [{Foo: "Bar"}, ...]
mappedStates = states.map(state => { return { [state.state]: state.name } })
// Step 2: Merge all the objects in that array into a single new object
statesObj = Object.assign({}, ...mappedStates)
Step 1 uses map
to iterate over every item in the array (every state object). map
executes a function for every state
object and returns a new object with the state as the key and the name as the value. We need to surround state.state
in brackets because it's a dynamic value in an object literal.
步骤 1 用于map
迭代数组中的每个项目(每个状态对象)。map
为每个state
对象执行一个函数并返回一个以状态为键、名称为值的新对象。我们需要state.state
用括号括起来,因为它是对象字面量中的动态值。
Step 2 uses Object.assign
to merge all the new state objects in the mappedStates
array into a new object (the first parameter, {}
).
What are the three dots ...
for? That's the spread operator. It takes every element in the mappedStates
array and turns them into direct arguments of the Object.assign
method.
步骤 2 用于Object.assign
将mappedStates
数组中的所有新状态对象合并为一个新对象(第一个参数,{}
)。三个点...
有什么用?那就是价差运算符。它获取mappedStates
数组中的每个元素并将它们转换为Object.assign
方法的直接参数。
This example makes it clear:
这个例子很清楚:
Object.assign({}, ...mappedStates)
is the same as
是相同的
Object.assign({}, {AZ: "Arizona"}, {NY: "New York"})
That's it!
而已!
回答by Adam Boduch
Here's a shorter version:
这是一个较短的版本:
_.transform(array, _.ary(_.extend, 2), {});
The transform()function is like reduce(), except it's not expecting you to return anything. Since extend()is altering it's first argument, we can just pass it straight to transform()
. It's wrapped in ary()to make sure it only gets 2 arguments passed to it.
该变换()函数就像是减少() ,但它没有指望你任何回报。由于extend()正在改变它的第一个参数,我们可以直接将它传递给transform()
. 它包含在ary() 中,以确保它只传递 2 个参数。
回答by udnisap
Use fromPairs on lodash 4. https://lodash.com/docs#fromPairs
使用fromPairs上lodash 4 https://lodash.com/docs#fromPairs
_.fromPairs([['fred', 30], ['barney', 40]]);
_.fromPairs([['fred', 30], ['barney', 40]]);