const data = [
{id:'xx', value: 1},
{id:'xx', value: 1},
{id:'yyy', value: 1},
{id:'yy', value: 1}
]
const map = {} const expectData = [] data.forEach((obj, idx) => { if (!map[obj.id]) { expectData.push({ id: obj.id, data: [obj] }) map[obj.id] = obj } else { expectData.forEach((iem, i) => { if (iem.id === obj.id) { iem.data.push(obj) } }) } }) console.log(expectData)