如何将对象数组转换为 Lodash 中的对象?
How to convert an array of objects to an object in Lodash?
我有这个:
[ { list:
[ [Object],
[Object] ] },
{ head:
[ [Object],
[Object] ] }
]
想把它变成这样:
{ list:
[ [Object],
[Object] ],
head: [ [Object],
[Object] ]
}
于是一个对象数组变成了一个对象。如果能用 lodash 实现这一点就太好了。
_.reduce(array, function(memo, current) { return _.assign(memo, current) }, {})
这是一个较短的版本:
_.transform(array, _.ary(_.extend, 2), {});
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() 以确保它只获得传递给它的 2 个参数。
我认为更短的解决方案是:
Object.assign({}, ...array)
我知道您需要 lodash,但您似乎甚至不需要这种方式。除非你想使用 _.extend
.
在 lodash 4 上使用 fromPairs。https://lodash.com/docs#fromPairs
_.fromPairs([['fred', 30], ['barney', 40]]);
以@rcsole 的出色答案为基础,这很有效:
states = [{
state: "NY",
name: "New York",
}, {
state: "AZ",
name: "Arizona",
}]
statesObj = Object.assign({}, ...states.map(state => {
return { [state.state]: state.name }
}))
结果:
{
AZ: "Arizona",
NY: "New York",
}
这是怎么回事?
让我们把它分成多个部分:
// 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)
第 1 步使用 map
遍历数组中的每个项目(每个状态对象)。 map
为每个 state
对象和 returns 一个以状态为键、名称为值的新对象执行一个函数。我们需要将 state.state
括在方括号中,因为它是对象字面量中的动态值。
第2步使用Object.assign
将mappedStates
数组中的所有新状态对象合并为一个新对象(第一个参数,{}
)。
三个点 ...
是做什么用的?这就是传播运算符。它获取 mappedStates
数组中的每个元素并将它们转换为 Object.assign
方法的直接参数。
这个例子很清楚:
Object.assign({}, ...mappedStates)
与
相同
Object.assign({}, {AZ: "Arizona"}, {NY: "New York"})
就是这样!
我有这个:
[ { list:
[ [Object],
[Object] ] },
{ head:
[ [Object],
[Object] ] }
]
想把它变成这样:
{ list:
[ [Object],
[Object] ],
head: [ [Object],
[Object] ]
}
于是一个对象数组变成了一个对象。如果能用 lodash 实现这一点就太好了。
_.reduce(array, function(memo, current) { return _.assign(memo, current) }, {})
这是一个较短的版本:
_.transform(array, _.ary(_.extend, 2), {});
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() 以确保它只获得传递给它的 2 个参数。
我认为更短的解决方案是:
Object.assign({}, ...array)
我知道您需要 lodash,但您似乎甚至不需要这种方式。除非你想使用 _.extend
.
在 lodash 4 上使用 fromPairs。https://lodash.com/docs#fromPairs
_.fromPairs([['fred', 30], ['barney', 40]]);
以@rcsole 的出色答案为基础,这很有效:
states = [{
state: "NY",
name: "New York",
}, {
state: "AZ",
name: "Arizona",
}]
statesObj = Object.assign({}, ...states.map(state => {
return { [state.state]: state.name }
}))
结果:
{
AZ: "Arizona",
NY: "New York",
}
这是怎么回事?
让我们把它分成多个部分:
// 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)
第 1 步使用 map
遍历数组中的每个项目(每个状态对象)。 map
为每个 state
对象和 returns 一个以状态为键、名称为值的新对象执行一个函数。我们需要将 state.state
括在方括号中,因为它是对象字面量中的动态值。
第2步使用Object.assign
将mappedStates
数组中的所有新状态对象合并为一个新对象(第一个参数,{}
)。
三个点 ...
是做什么用的?这就是传播运算符。它获取 mappedStates
数组中的每个元素并将它们转换为 Object.assign
方法的直接参数。
这个例子很清楚:
Object.assign({}, ...mappedStates)
与
相同Object.assign({}, {AZ: "Arizona"}, {NY: "New York"})
就是这样!