【发布时间】:2017-02-07 16:55:09
【问题描述】:
我有一个对象数组:
` tempArray = [
{ name: 'Lion-O' },
{ gender: 'Male' },
{ weapon: 'Sword of Omens' },
{ status: 'Lord of the Thundercats' },
]
`
我想转换成的对象:
`{
name: 'Lion-O',
gender: 'Male,',
weapon: 'Sword of Omens',
status: 'Lord of the Thundercats'
}`
我尝试在 LoDash 中使用 reduce;
const tempObj = _.reduce(tempArray, (r, v, k) => {
return r
})
console.log(tempObj);
//=> { name: 'Lion-O' }
我不确定我应该如何迭代数组?看了Doc's他们的例子显示添加或推送到数组..我只想要一个对象..我知道它可以完成。如果他们是更好的方式,我也愿意接受
提前致谢。
【问题讨论】:
标签: javascript arrays object lodash