could you help me? I have this JSON and I need to group in a new object by key:
[
{
"product": "name 1",
"price": "3000",
"inspection": false,
},
{
"product": "name 2",
"price": "1000",
"inspection": true,
},
{
"product": "name 3",
"price": "5000",
"inspection": false,
},
]
Expected Final Result:
{
"product": ["name 1","name 2","name 3"],
"price": ["3000","1000","5000"],
"inspection": [false,true,false]
}
I tried to use -> for, foreach <- but not getting a good/optimum result… would you mind helping me, please? (was thinking about reduce, but I didn’t make it work)
>Solution :
let data = [
{
"product": "name 1",
"price": "3000",
"inspection": false,
},
{
"product": "name 2",
"price": "1000",
"inspection": true,
},
{
"product": "name 3",
"price": "5000",
"inspection": false,
},
];
let output = Object.fromEntries(
Object.keys(data[0]).map(k =>
[k, data.map(d => d[k])]
)
);
console.log(output);