Let's say I have an array:
var myArr = [
{a: {'one': 1} },
{b: {'two': 2} },
{a: {'three': 3} },
{c: {'four': 4} },
{d: {'five': 5} }
];
I want to create an object so that:
let myObj = {};
myObj = {
a: {
'one': 1,
'three': 3
},
b: {'two': 2},
c: {'four': 4},
d: {'five': 5}
}
The property 'a'becomes overridden. How to prevent this?
The problem I am facing is that I am doing the following:
myArr.forEach((x) => {
myObj[Object.keys(x)[0]] = x[Object.keys(x)];
});
I get the result:
{
"a": {"three": 3},
"b": {"two": 2},
"c": {"four": 4},
"d": {"five": 5}
}
source
share