const list = [
{ unit: '瓶', count: 1 }, { unit: '瓶', count: 2 },
{ unit: '箱', count: 3 }, { unit: '箱', count: 4 },
{ unit: '盒', count: 5 }, { unit: '盒', count: 6 },
]
let allKeys = []
for (var value of list) {
allKeys.push(value.unit)
}
const sameKeys = [...new Set(allKeys)]
console.log("🚀 ~ file: 1.jsx ~ line 12 ~ sameKeys", sameKeys)
const sameKeysCount = sameKeys.map(item => {
return { unit: item, count: 0 }
})
for (var item of list) {
const index = pros.findIndex((element) => element.unit === item.unit);
sameKeysCount[index].count += item.count
}
console.log("🚀 ~ file: 1.jsx ~ line 17 ~ sameKeysCount", sameKeysCount)
网友评论