1 回答

TA貢獻1872條經(jīng)驗 獲得超4個贊
您應該能夠為此目的使用Array.reduce 。
我們創(chuàng)建一個映射,以 pid 為鍵,然后為每個已完成或待處理的對象加 1。 然后Object.values會再次將此映射轉換為數(shù)組。我希望這可以幫助你!
data = [
{
"pid": 1,
"status": "done",
"title": "a"
},
{
"pid": 1,
"status": "pending",
"title": "a"
},
{
"pid": 1,
"status": "pending",
"title": "21"
},
{
"pid": 2,
"status": "pending",
"title": "c"
}
]
let result = Object.values(data.reduce( (map, d) => {
if (!map[d.pid]) map[d.pid] = { pid: d.pid, done: 0, pending: 0 };
if (d.status === "done") map[d.pid].done++;
if (d.status === "pending") map[d.pid].pending++;
return map;
}, {} ));
console.log("Result:", result);
添加回答
舉報