'how to convert multi element object to one element object
I have object like this
[
{ A: '1' },
{ B: '2' },
{ C: '3' },
]
I want convert to like this
{
A: '1',
B: '2',
C: '3',
}
what is the best way to do it
Solution 1:[1]
- Object.assign with spread syntax
let arr = [
{ A: '1' },
{ B: '2' },
{ C: '3' },
]
console.log(Object.assign(...arr));
Solution 2:[2]
let arr = [
{ A: '1' },
{ B: '2' },
{ C: '3' },
]
let output = {}
arr.map((obj) => {
output = {...output, ...obj}
})
console.log(output)
Solution 3:[3]
Object.assign and spread operator will do the trick :
let arrObject = [
{ A: "1" },
{ B: "2" },
{ C: "3" }
];
Object.assign(target, ...sources)
let obj = Object.assign({}, ...arrObject);
console.log(obj) => obj = { A: '1', B: '2', C: '3' }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dreamy Player |
| Solution 2 | dinesh oz |
| Solution 3 | Diop Nikuze |
