当前,我正在尝试计算对象数组中的多次出现并将最终计数推入其中。我不想将数据存储在其他数组中。数据应保留在现有数据中。
我要添加计数的数组:
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);
答案 0 :(得分:6)
您可以先遍历所有项目,然后将旧对象和新的count属性分配给新对象,从而获得计数。
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; }
答案 1 :(得分:2)
您可以按照以下步骤进行操作:
reduce()
从数组创建对象,该对象具有所有唯一艺术家的数量artists
不同的键,其值将作为其计数。forEach
。count
设置为计数数组中当前项目的演出者的值。
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' }
];
const unique = array.reduce((ac,{artist:a}) => (ac[a] = ac[a] + 1 || 1,ac),{});
array.forEach(x => x.count = unique[x.artist]);
console.log(array)
答案 2 :(得分:1)
您可以对数组进行一次遍历,以建立艺术家姓名映射到计数,然后进行第二遍修改每个数组项以添加与艺术家相关的计数。
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 counts = array.reduce((counts, item) => {
var artistName = item.artist;
if (counts[artistName]) {
counts[artistName] += 1;
} else {
counts[artistName] = 1;
}
return counts;
}, {});
array.forEach(item => item.count = counts[item.artist])
console.log(array);
为清楚起见,.reduce
函数很冗长,但可以大大缩短:
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 counts = array.reduce((counts, item) => (counts[item.artist] = counts[item.artist] || 1, counts), {});
console.log(counts);
如果要创建一个 new 数组而不是修改旧数组中的对象,则可以复制每个对象和该数组:
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 counts = {
"metallica": 3,
"foofighters": 2,
"deftones": 1,
"deichkind": 1
}
var newArray = array.map(item => ({...item, count: counts[item.artist]}))
console.log(newArray);
console.log(array);
答案 3 :(得分:0)
您可以在数组上迭代两次。
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.forEach(a => {
if (!a.hasOwnProperty('count')) {
Object.assign(a, { count: 0 });
}
array.forEach(b => {
if (a.artist === b.artist) {
a.count++;
}
});
});
console.log(array);