简体   繁体   English

如何在对象数组中找到多个匹配项并添加计数值?

[英]How to find multiple occurrences in an array of objects and add count value?

Currently I am trying to count multiple occurrences inside an array of objects and push a final counting into it. 目前我正在尝试计算对象数组中的多次出现并将最终计数推入其中。 I do not want to have the data storred in an additional array. 我不希望将数据存储在其他数组中。 The data should stay in the existing one. 数据应保留在现有数据中。

The array I'd try to add the 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' }
];

My current example code deletes/reduces from the array so the final result is not as desired: 我当前的示例代码从数组中删除/减少,因此最终结果不符合要求:

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 }

I am trying to achieve the result as: 我试图达到以下结果:

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 }
];

Any help is appreciated to point me in the right direction. 感谢任何帮助指出我正确的方向。

Thank you for helping! 感谢您的帮助! The desired solution is: 理想的解决方案是:

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);

You could get the count first by iterating over all items and then assign to a new object the old object and a new count property. 您可以通过迭代所有项目来获取计数,然后将旧对象和新计数属性分配给新对象。

 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; } 

You can do that in following steps: 您可以通过以下步骤执行此操作:

  • Create an object from array using reduce() which have count of all the unique artists 使用reduce()从数组创建一个对象,其中包含所有唯一艺术家的数量
  • The object will have keys which will different artists and their values will be their count. 该对象将具有不同的artists及其价值将成为他们的计数的键。
  • Then use forEach on the original array. 然后在原始数组上使用forEach
  • Set the count of all the to the value of artist of current item in count array. 将所有的count设置为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) 

You can do one pass over the array building a map of artist name to a count, and a second pass to modify each array item adding the count associated with the artist. 您可以在数组上执行一次传递,将艺术家名称的地图构建为计数,第二次传递以修改每个数组项目,添加与艺术家关联的计数。

 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); 

The .reduce function is verbose for clarity but it can be shortened a lot: 为清晰起见, .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); 

If you want to create a new array instead of modifying the objects in the old one, then you can make a copy of each object and the 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' } ]; 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); 

You can iterate two times over the 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' }
];

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM