Sorry about the title. I don't even know how I can explain what I want.
So here is what I am trying to achieve
const array = [
{
key: 0,
key2: [ { id: "a", data: "abc" }, { id: "b", data: "wxy" }... ]
},
{
key: 1,
key2: [ { id: "a", data: "qwe" }, { id: "b", data: "zxc" }... ]
},
...
]
I want to convert it to,
const result = {
0 : {
a: "abc",
b: "wxy"
},
1 : {
a: "qwe",
b: "zxc"
}
}
so far, I have this:
const transforms = array
.map((o) => {
return { [o.key]: o.key2 };
})
.reduce((prev, curr) => {
for (let key in curr)
prev[key] = curr[key]
.map((c) => {
return { [c.id]: c.data};
})
.reduce((prev, curr) => {
for (let key in curr) prev[key] = curr[key];
return prev;
}, {});
return prev;
}, {});
Which is hard to read, and probably not very performant. To be honest, I don't even know if it is really 100% working. It gave me expected result so far.
How do I refactor this? Please help.