将多个对象与对象数组中的值之和合并,并在 javascript 中重新排列数组
merge multiple objects with sum of values from object array and rearrange the array in javascript
我有这样的数组:
[{"an": "0",
"ap": "11",
"status":"active"
"zr":"55"},
{"an": "0",
"ap": "15",
"status":"death"
"zr":"55"},
{"an": "0",
"ap": "18",
"status":"recovered"
"zr":"54"},
{"an": "0",
"ap": "11",
"status":"active"
"zr":"55"},
{"an": "0",
"ap": "11",
"status":"death"
"zr":"55"},
{"an": "0",
"ap": "11",
"status":"recovered"
"zr":"55"},
{"an": "0",
"ap": "11",
"status":"active"
"zr":"55"},
{"an": "0",
"ap": "11",
"status":"death"
"zr":"55"},
{"an": "0",
"ap": "11",
"status":"recovered"
"zr":"55"} ]
我想修改这个数组,所以修改后的数组应该是这样的
[ {"state":"an","active":"0", "death":"0", "recovered":"0"},{"state":"ap","active":"33", "death":"36", "recovered":"40"},{"state":"zr","active":"165", "death":"165", "recovered":"165"}]
我试过根据状态groupby数据之类的方法
var arr={};
var result=data.map(({status, ...item}) =>{
Object.keys(item).map((keys)=>{
arr.state=keys;
arr[status]= arr[status] ? arr[status] +item[keys] : item[keys];
});
return arr;
})
console.log(arr);
谁能告诉我哪里出错了?
我想要 javascript 中的通用代码,如果我要添加多个键,它也适用。
提前谢谢你。
您可以按 status
分组,然后将其余对象作为值。
var data = [{ an: "0", ap: "11", status: "active", zr: "55" }, { an: "0", ap: "15", status: "death", zr: "55" }, { an: "0", ap: "18", status: "recovered", zr: "54" }, { an: "0", ap: "11", status: "active", zr: "55" }, { an: "0", ap: "11", status: "death", zr: "55" }, { an: "0", ap: "11", status: "recovered", zr: "55" }, { an: "0", ap: "11", status: "active", zr: "55" }, { an: "0", ap: "11", status: "death", zr: "55" }, { an: "0", ap: "11", status: "recovered", zr: "55" }],
result = Object.values(data.reduce((r, { status, ...o }) => {
Object.entries(o).forEach(([state, v]) => {
r[state] = r[state] || { state };
r[state][status] = ((+r[state][status] || 0) + +v).toString();
});
return r;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
我有这样的数组:
[{"an": "0",
"ap": "11",
"status":"active"
"zr":"55"},
{"an": "0",
"ap": "15",
"status":"death"
"zr":"55"},
{"an": "0",
"ap": "18",
"status":"recovered"
"zr":"54"},
{"an": "0",
"ap": "11",
"status":"active"
"zr":"55"},
{"an": "0",
"ap": "11",
"status":"death"
"zr":"55"},
{"an": "0",
"ap": "11",
"status":"recovered"
"zr":"55"},
{"an": "0",
"ap": "11",
"status":"active"
"zr":"55"},
{"an": "0",
"ap": "11",
"status":"death"
"zr":"55"},
{"an": "0",
"ap": "11",
"status":"recovered"
"zr":"55"} ]
我想修改这个数组,所以修改后的数组应该是这样的
[ {"state":"an","active":"0", "death":"0", "recovered":"0"},{"state":"ap","active":"33", "death":"36", "recovered":"40"},{"state":"zr","active":"165", "death":"165", "recovered":"165"}]
我试过根据状态groupby数据之类的方法
var arr={};
var result=data.map(({status, ...item}) =>{
Object.keys(item).map((keys)=>{
arr.state=keys;
arr[status]= arr[status] ? arr[status] +item[keys] : item[keys];
});
return arr;
})
console.log(arr);
谁能告诉我哪里出错了? 我想要 javascript 中的通用代码,如果我要添加多个键,它也适用。 提前谢谢你。
您可以按 status
分组,然后将其余对象作为值。
var data = [{ an: "0", ap: "11", status: "active", zr: "55" }, { an: "0", ap: "15", status: "death", zr: "55" }, { an: "0", ap: "18", status: "recovered", zr: "54" }, { an: "0", ap: "11", status: "active", zr: "55" }, { an: "0", ap: "11", status: "death", zr: "55" }, { an: "0", ap: "11", status: "recovered", zr: "55" }, { an: "0", ap: "11", status: "active", zr: "55" }, { an: "0", ap: "11", status: "death", zr: "55" }, { an: "0", ap: "11", status: "recovered", zr: "55" }],
result = Object.values(data.reduce((r, { status, ...o }) => {
Object.entries(o).forEach(([state, v]) => {
r[state] = r[state] || { state };
r[state][status] = ((+r[state][status] || 0) + +v).toString();
});
return r;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }