I have an objects inside parent object. Can I create an object in which there are these objects?
let a = {
"11111": {"1": "test", "11": "test"},
"22222": {"2": "test", "22": "test"}
}
Expected output:
{
"1": "test",
"11": "test",
"2": "test",
"22": "test",
}
>Solution :
Just reduce the object values by spreading them onto a new object.
const
a = {
"11111": { "1": "test", "11": "test" },
"22222": { "2": "test", "22": "test" }
},
b = Object.values(a).reduce((acc, obj) => ({ ...acc, ...obj }), {});
console.log(b);
If you want to be efficient, you can call Object.assign instead:
const
a = {
"11111": { "1": "test", "11": "test" },
"22222": { "2": "test", "22": "test" }
},
b = Object.values(a).reduce((acc, obj) => Object.assign(acc, obj), {});
console.log(b);