const arr = [
{
id: 1,
group: 1,
name: 'Ivan'
},
{
id: 2,
group: 1,
name: 'Ivan'
},
{
id: 3,
group: 1,
name: 'Ivan'
},
{
id: 4,
group: 2,
name: 'Ivan'
}
];
const output = [
{
people: [
{
id: 1,
group: 1,
name: 'Ivan'
},
{
id: 2,
group: 1,
name: 'Ivan'
},
{
id: 3,
group: 1,
name: 'Ivan'
}
],
groupId: 1
},
{
people: [
{
id: 4,
group: 2,
name: 'Ivan'
}
],
groupId: 2
}
];
const arr = [
{
id: 1,
group: 1,
name: 'Ivan'
},
{
id: 2,
group: 1,
name: 'Ivan'
},
{
id: 3,
group: 1,
name: 'Ivan'
},
{
id: 4,
group: 2,
name: 'Ivan'
}
];
const map = new Map();
arr.forEach((item)=>{
let group = map.get(item.group) || {groupId:item.group, people:[]}
group.people.push(item)
map.set(item.group, group)
});
const output = Array.from(map.values());