1 回答

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊
我猜contents你想要的是一個(gè)數(shù)組而不是一個(gè)對(duì)象,因?yàn)橥粋€(gè)對(duì)象中不應(yīng)該有相同的屬性。
也許這會(huì)對(duì)你有所幫助。
const rows = [
{"m_idx": 1, "m_name": "a"},
{"m_idx": 1, "m_name": "b"},
{"m_idx": 2, "m_name": "c"}
]
const map = new Map();
rows.forEach(row => {
if (map.get(row.m_idx) === undefined) {
map.set(row.m_idx, {
"m_idx": row.m_idx,
"contents": [{"m_name": row.m_name}]
});
} else {
map.get(row.m_idx).contents.push({"m_name": row.m_name});
}
});
const result = [];
map.forEach((v, k) => {
result.push(v);
});
console.log(result);
添加回答
舉報(bào)