简体   繁体   English

Javascript比较多个对象数组

[英]Javascript compare multiple arrays of objects

How can I compare multiple arrays of objects and add new properties with the number of occurrences an object was found and the array indexes where the object was found? 如何比较多个对象数组并添加新属性以及找到对象的次数以及找到对象的数组索引? The object comparison must be made by the name property. 对象比较必须由name属性进行。

Example: 例:

var arrays = [
  [
    {
      name: 'aa',
      value: 1
    },
    {
      name: 'ab',
      value: 2
    },
    {
      name: 'ac',
      value: 3
    },
    {
      name: 'aa',
      value: 1
    }
  ],
  [
    {
      name: 'aa',
      value: 1
    },
    {
      name: 'ab',
      value: 2
    },
  ],
  [
    {
      name: 'ac',
      value: 3
    },
    {
      name: 'aa',
      value: 1
    }
  ]
]

After execution the object from the above array should have these properties: 执行后,上面数组中的对象应具有以下属性:

[
  [
    {
      name: 'aa',
      value: 1,
      occurrences: 3,
      where: [0, 1, 2]
    },
    {
      name: 'ab',
      value: 2,
      occurrences: 2,
      where: [0, 1]
    },
    {
      name: 'ac',
      value: 3,
      occurrences: 2,
      where: [0, 2]
    },
    {
      name: 'aa',
      value: 1,
      occurrences: 3,
      where: [0, 1, 2]
    }
  ],
  [
    {
      name: 'aa',
      value: 1,
      occurrences: 3,
      where: [0, 1, 2]
    },
    {
      name: 'ab',
      value: 2,
      occurrences: 2,
      where: [0, 1]
    }
  ],
  [
    {
      name: 'ac',
      value: 3,
      occurrences: 2,
      where: [0, 2]
    },
    {
      name: 'aa',
      value: 1,
      occurrences: 3,
      where: [0, 1, 2]
    }
  ]
]

Basically I want to check if the object with a specific name property exists in the other arrays. 基本上我想检查具有特定name属性的对象是否存在于其他数组中。

This is the solution that comes in my mind: 1. Loop through the array that has the most objects 这是我脑海中的解决方案:1。遍历具有最多对象的数组
2. Loop through each object 2.遍历每个对象
3. Loop through the other arrays and apply Array.prototype.find() 3.遍历其他数组并应用Array.prototype.find()
But this will take a lot of time since each of my array will have at least 500 objects... 但是这需要花费很多时间,因为我的每个阵列都至少有500个对象......

This looked like trivial reduce, until i noticed nested arrays ) so it's more like flatten + reduce, with memory. 这看起来像是微不足道的减少,直到我注意到嵌套数组)所以它更像是flatten + reduce,带有内存。

Code below is doing what you need, just the prop names are short (as i type them on the phone): 下面的代码正在做你需要的,只是道具名称很短(因为我在手机上键入它们):

let f = (ai, a,v,i,m) => {
    if (!a[v.id]) {
    a[v.id] = {id: v.id, v: v.name, count: 1, at: [ai]};
    } else {
    a[v.id].count += 1;
    a[v.id].at.push(ai);
    }
    return a;
};
let r = [[{id: 'aa', value: 42}], [{id: 'ba', value: 11}, {id: 'aa', value: 42}]]
.reduce ((a, v, i) => v.reduce (f.bind (null, i),a), {}); 
console.log (r);

Code still visits each element in any array only once, so complexity is O(n), and there should not be a problem running it on arrays up to a million of total elements (eg 1000 arrays of 1000 elements, or 200 x 5000). 代码仍然只访问任何数组中的每个元素一次,因此复杂度为O(n),并且在阵列上运行它时应该没有问题,最多可达一百万个元素(例如,1000个元素的1000个数组,或200 x 5000) 。

You can use array#reduce to get the number of occurrences and the index of occurrences in an object. 您可以使用array#reduce来获取对象中出现次数和出现次数的索引。

Then, you can modify your object in the arrays by simply using Object.assign() and adding the where and occurrences property. 然后,您可以通过简单地使用Object.assign()并添加whereoccurrences属性来修改arrays的对象。

 var arrays = [ [ { name: 'aa', value: 1 }, { name: 'ab', value: 2 }, { name: 'ac', value: 3 }, { name: 'aa', value: 1 } ], [ { name: 'aa', value: 1 }, { name: 'ab', value: 2 }, ], [ { name: 'ac', value: 3 }, { name: 'aa', value: 1 } ] ]; var result = arrays.reduce((res, arr, index) => { arr.forEach(({name,value}) => { res[name] = res[name] || {occurrences: 0}; res[name]['where'] = res[name]['where'] || []; if(!res[name]['where'].includes(index)){ res[name]['where'].push(index); res[name].occurrences += 1; } }); return res; },{}); arrays.forEach(arr => arr.forEach(obj => Object.assign(obj, result[obj.name]))); console.log(arrays); 

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

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