【发布时间】:2019-08-23 11:11:35
【问题描述】:
我有这个对象数组
[{
tag: 'james'
},
{
tag: 'james'
},
{
tag: 'john'
}
]
如何计算并生成如下所示的新数组?
[{
tag: 'james',
count: 2
}, {
tag: 'john',
count: 1
}]
我尝试使用 reduce 生成的对象而不是对象数组。
const arr = [{tag: 'james'},{tag: 'james'},{tag: 'john'}];
let newArr = arr.reduce((accum, arr) => {
accum[arr.tag] = ++accum[arr.tag] || 1
return accum
}, {})
console.log(newArr)
【问题讨论】:
标签: javascript arrays reactjs object ecmascript-6