Just looking for the cleanest way to turn the following array into the following object format. Thanks a lot
const item = [
{ address: '123 fake street' },
{ loan: 'no' },
{ property: 'no' }
]
const obj = {
address: '123 fake street',
loan: 'no',
property: 'no'
}
You can use Object.assign() and spread syntax to convert the array of objects into a single object.
const item = [
{ address: '123 fake street' },
{ loan: 'no' },
{ property: 'no' }
]
const obj = Object.assign({}, ...item);
console.log(obj);
Reduce and spread syntax would be one clean way to convert the array to an object.
const item = [
{ address: '123 fake street' },
{ loan: 'no' },
{ property: 'no' }
]
let obj = item.reduce((pre, cur)=>{
return {...pre, ...cur};
}, {});
// Result: obj={address: '123 fake street', loan: 'no', property: 'no'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With