我有一个数组,数组格式如下:
[
{
id: 1,
name: 1,
msg: {
masterId: 1,
stackId: 2
}
},
{
id: 2,
name: 2,
msg: {
masterId: 1,
stackId: 2
}
},
{
id: 3,
name: 3,
msg: {
masterId: 3,
stackId: 3
}
},
{
id: 4,
name: 4
}
]
想要根据msg中的masterId和stackId进行分组,masterId标识数据属于父元素,如果masterId和id相同,说明该元素为父元素,stackId标识这个设备属于一个组,最后想得到的格式如下
[
{
id: 1,
name: 1,
msg: {
masterId: 1,
stackId: 2
},
children: [
{
id: 2,
name: 2,
msg: {
masterId: 1,
stackId: 2
}
}
]
},
{
id: 3,
name: 3,
msg: {
masterId: 3,
stackId: 3
}
},
{
id: 4,
name: 4
}
]
请问该怎么做?