I have a Array of Objects:
` tempArray = [
{ name: 'Lion-O' },
{ gender: 'Male' },
{ weapon: 'Sword of Omens' },
{ status: 'Lord of the Thundercats' },
]
`
Object I want to Transform into:
`{
name: 'Lion-O',
gender: 'Male,',
weapon: 'Sword of Omens',
status: 'Lord of the Thundercats'
}`
I tried to use reduce in LoDash;
const tempObj = _.reduce(tempArray, (r, v, k) => {
return r
})
console.log(tempObj);
//=> { name: 'Lion-O' }
I'm not sure how I should iterate over the Array? Looked at Doc's their example shows Adding or Pushing onto a Array .. I just want a Object.. I know it can be done. If their is a better way I'm open to that as well
Thanks in advance.
Shorter equivalent solution :
const tempArray = [
{ name: 'Lion-O' },
{ gender: 'Male' },
{ weapon: 'Sword of Omens' },
{ status: 'Lord of the Thundercats' },
];
const newObj = Object.assign({}, ...tempArray);
console.log(newObj);
// Object {name: "Lion-O", gender: "Male", weapon: "Sword of Omens", status: "Lord of the Thundercats"}
tempArray = [ { name: 'Lion-O' }, { gender: 'Male' }, { weapon: 'Sword of Omens' }, { status: 'Lord of the Thundercats' }, ] var newObject = {}; for (var index in tempArray) { thisObject = tempArray[index]; for (var key in thisObject) { newObject[key] = thisObject[key]; } } console.log(newObject);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.