我有一个数组的数组,如下所示。使用ES6语法,我该如何将每个值
Good
、Excellent
和Wow
的数量统计到一个新数组中,例如[{name: Good, count: 4} {name: Excellent, count: 5}, {name:Wow, count:2}]
?我尝试使用Object.assign
,但是我无法将关键字计数“唯一化”,相反,我需要使用数组,因为我正在尝试在前端渲染它。我需要使用reduce
吗?怎么用?
let k = 0
const stats = {}
const remarks = [
[{name: "Good"}],
[{name: "Good"}, {name: "Excellent"}],
[{name: "Good"}, {name: "Excellent"}, {name: "Wow"}],
[{name: "Good"}, {name: "Excellent"}, {name: "Wow"}],
[{name: "Excellent"}],
[{name: "Excellent"}]
]
remarks.forEach((arr) => {
arr.map((e) => {
Object.assign(stats, { [e.name]: k = k + 1 })
})
})
console.log(stats);
输出:
stats: {Good: 8, Excellent: 11, Wow: 9}
这是不正确的,我需要使用一个数组。
期望的输出:
[{name: Good, count: 4} {name: Excellent, count: 5}, {name:Wow, count:2}]