简体   繁体   English

将计数值添加到具有相同键值的对象(对象数组的数组)

[英]Add count value to object with same key values (array of array of objects)

Hello I have kind of complicated iteration to be done over an array of array of objects.您好,我要对一组对象进行复杂的迭代。 I have array like this in Javascript:我在 Javascript 中有这样的数组:

arr = [
 [{ apiName: 'app/abc', error: '', res: 'response' }],
 [{ apiName: 'app/abc', error: '', res: 'response' }],
 [{ apiName: 'app/abc', error: '', res: 'response' }],
 [{ apiName: 'app/abc', error: 'error's', res: 'response' }],
 [{apiName: 'app/abc', error: 'no error's', res: 'response' }],
 [{apiName: 'app/abc', error: 'no error's', res: 'response' }],
 [{ apiName: 'app/abc', error: '', res: 'response' }],
]

I would like to add count property to each object that counts objects with same name and surname... So it should be now:我想为每个对象添加 count 属性,以计算具有相同名称和姓氏的对象......所以现在应该是:

[
 { apiName: 'app/abc', error: '', count: 4 },
 { apiName: 'app/abc', error: 'Sheth', count: 2 },
 { apiName: 'app/abc', error: 'Sen', count: 1' },
]

Here is my code:这是我的代码:

// It works only for array of objects
let summary;
for(i=0; i<arr.length; i++){
 summary = Object.values(arr[i].reduce((a,{apiName, error}) => {
  let k = `${apiName}_${error}`;
  a.push(a[k] = a[k] || {apiName, error, count : 0});
  a[k].count++;
  return a;
 },[]));
}

You need to reduce the nested data structure and omit pushing to the common key.您需要减少嵌套数据结构并省略推送到公共键。 Instead use an object with the key and count.而是使用带有键和计数的对象。

 const data = [[{ apiName: 'app/abc', error: '', res: 'response' }], [{ apiName: 'app/abc', error: '', res: 'response' }], [{ apiName: 'app/abc', error: '', res: 'response' }], [{ apiName: 'app/abc', error: 'error', res: 'response ' }], [{ apiName: 'app/abc', error: 'no error', res: 'response' }], [{ apiName: 'app/abc', error: 'no error', res: 'response ' }], [{ apiName: 'app/abc', error: '', res: 'response' }]], summary = Object.values(data.reduce((r, array) => array.reduce((a, { apiName, error }) => { const key = `${apiName}_${error}`; a[key]??= { apiName, error, count: 0 }; // or a[key] = a[key] || { apiName, error, count: 0 }; a[key].count++; return a; }, r), [])); console.log(summary);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

暂无
暂无

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

相关问题 将计数值添加到具有相同键值的对象(对象数组) - Add count value to object with same key values (array of objects) 如果一个键相同,则创建新的对象数组,然后添加这些对象的其他键值 - Create new array of objects if one key is same then add other key's value of those object 从数组中的键向对象数组 object 添加键和值 - Add key and value to array of objects object from the keys in array 如何对相同键的值求和并在每个对象的对象数组中添加一个键,该键的值包含 id 相同键的数组? - how to sum value of same keys and add a key with value that contains array of id same keys, in array of objects for each object? 如果具有相同的键值,则将键值添加到对象数组 - Add key value to an object array if it has same key value Javascript:将对象添加到具有相同键的现有对象数组不起作用 - Javascript: Add object to Existing Array of objects with the same key not working 具有数组键的对象数组需要计算数组中作为键的每个值 - Array of objects with key that is an array need to count each value in the array that is a key object 数组中相同值的计数 - Count for same value in array of object 合并具有相同键值对的对象数组中的值 - merge values inside array of objects with the same key-value pair 将数组转为对象,并从另一个 object 添加键值 - Turn array into objects, and add key values from another object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM