JSFiddle - React, Tailwind, and code Playground
by Eugen Sunic
JavaScript
const srcArr = [{
"1": {
"60": {
"point": 10,
"count": 1
},
"68": {
"point": 20,
"count": 1
},
}
},
{
"2": {
"60": {
"point": 100,
"count": 2
}
}
},
{
"1": {
"88": {
"point": 50,
"count": 1
},
"68": {
"point": 20,
"count": 1
},
}
}
]
/* const result = srcArr.reduce((acc, x) => {
const key = Object.keys(x)[0];
if (!acc[key]) {
acc[key] = {
...acc[key],
x
}
} else {
acc[key] = {
...acc[key],
x
}
}
return acc;
}, {});
console.log(result)
*/
let res = srcArr.reduce((arr, x) => {
let k = Object.keys(x)[0];
const innerKeys = Object.keys(x[k]);
arr[k] = {
...arr[k],
...innerKeys.reduce((acc, y) => {
acc={'store':arr[k]}
if (!arr[k][y]) {
arr[k][y] = x;
} else {
arr[k][y]={...arr[k][y],point:arr[k][y][point]+acc.store.point}
}
return arr[k][y]
}, {})
}
return arr;
}, {});
console.log(res);