gpt4 book ai didi

javascript - 如何在对象数组中查找多次出现并添加计数值?

转载 作者:搜寻专家 更新时间:2023-11-01 05:03:13 24 4
gpt4 key购买 nike

目前,我正在尝试对对象数组中的多次出现进行计数,并将最终计数插入其中。我不想将数据存储在额外的数组中。数据应保留在现有数据中。

我尝试添加计数的数组:

var array = [
{ artist: 'metallica', venue: 'olympiastadion' },
{ artist: 'foofighters', venue: 'wuhlheide' },
{ artist: 'metallica', venue: 'columbiahalle' },
{ artist: 'deftones', venue: 'columbiahalle' },
{ artist: 'deichkind', venue: 'wuhlheide' },
{ artist: 'metallica', venue: 'wuhlheide' },
{ artist: 'foofighters', venue: 'trabrennbahn' }
];

我当前的示例代码从数组中删除/减少,因此最终结果不符合预期:

var array = [
{ artist: 'metallica', venue: 'olympiastadion' },
{ artist: 'foofighters', venue: 'wuhlheide' },
{ artist: 'metallica', venue: 'columbiahalle' },
{ artist: 'deftones', venue: 'columbiahalle' },
{ artist: 'deichkind', venue: 'wuhlheide' },
{ artist: 'metallica', venue: 'wuhlheide' },
{ artist: 'foofighters', venue: 'trabrennbahn' }
];

array = Object.values(array.reduce((r, { artist, venue }) => {
r[artist] = r[artist] || { artist, venue, count: 0 };
r[artist].count++;
return r;
}, {}));

console.log(array);

Which logs:
{ artist: 'metallica', venue: 'olympiastadion', count: 3 },
{ artist: 'foofighters', venue: 'wuhlheide', count: 2 },
{ artist: 'deftones', venue: 'columbiahalle', count: 1 },
{ artist: 'deichkind', venue: 'wuhlheide', count: 1 }

我正在尝试实现如下结果:

var array = [
{ artist: 'metallica', venue: 'olympiastadion', count: 3 },
{ artist: 'foofighters', venue: 'wuhlheide', count: 2 },
{ artist: 'metallica', venue: 'columbiahalle', count: 3 },
{ artist: 'deftones', venue: 'columbiahalle', count: 1 },
{ artist: 'deichkind', venue: 'wuhlheide', count: 1 },
{ artist: 'metallica', venue: 'wuhlheide', count: 3 },
{ artist: 'foofighters', venue: 'trabrennbahn', count: 2 }
];

感谢任何帮助,为我指明正确的方向。

感谢您的帮助!所需的解决方案是:

var array = [{ artist: 'metallica', venue: 'olympiastadion' }, { artist: 'foofighters', venue: 'wuhlheide' }, { artist: 'metallica', venue: 'columbiahalle' }, { artist: 'deftones', venue: 'columbiahalle' }, { artist: 'deichkind', venue: 'wuhlheide' }, { artist: 'metallica', venue: 'wuhlheide' }, { artist: 'foofighters', venue: 'trabrennbahn' }],
map = array.reduce(
(map, { artist }) => map.set(artist, (map.get(artist) || 0) + 1),
new Map
),
array = array.map(o => Object.assign({}, o, { count: map.get(o.artist) }));

console.log(array);

最佳答案

您可以先通过遍历所有项目来获取计数,然后将旧对象和新计数属性分配给新对象。

var array = [{ artist: 'metallica', venue: 'olympiastadion' }, { artist: 'foofighters', venue: 'wuhlheide' }, { artist: 'metallica', venue: 'columbiahalle' }, { artist: 'deftones', venue: 'columbiahalle' }, { artist: 'deichkind', venue: 'wuhlheide' }, { artist: 'metallica', venue: 'wuhlheide' }, { artist: 'foofighters', venue: 'trabrennbahn' }],
map = array.reduce(
(map, { artist }) => map.set(artist, (map.get(artist) || 0) + 1),
new Map
),
result = array.map(o => Object.assign({}, o, { count: map.get(o.artist) }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 如何在对象数组中查找多次出现并添加计数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56102119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com