<script>
let temp = [
{ id: "1", label: "1Msjjs-10-01", num: 10, name: "564" },
{ id: "2", label: "1Msjjs-20-01", num: 10, name: "789" },
{ id: "3", label: "1Msjjs-20-01", num: 20, name: "564" },
{ id: "4", label: "1Msjjs-10", num: 20, name: "564" },
{ id: "5", label: "1Msjjs-14", num: 20, name: "564" },
{ id: "6", label: "1Msjjs-15", num: 20, name: "564" },
{ id: "7", label: "1Msjjs-16", num: 20, name: "564" },
{ id: "8", label: "1Msjjs-16", num: 40, name: "564" },
{ id: "9", label: "1Msjjs-10-01", num: 20, name: "564" },
]
let arr = []
let obj = []
// 将相同label的进行分组
for (let i = 0; i < temp.length; i++) {
// 利用some函数来区分是否相同
if (!obj.some(item => item.label === temp[i].label)) {
obj.push(temp[i])
arr.push({
gname: temp[i].label,
data: [JSON.stringify(temp[i])],
})
} else {
// 把那些和键值相同的条目放入分组
for (let k = 0; k < arr.length; k++) {
if (arr[k].gname === temp[i].label) {
arr[k].data.push(JSON.stringify(temp[i]))
}
}
}
}
</script>
网友评论