简体   繁体   English

如何在匹配键值的基础上比较两个不同长度和键的数组?

[英]How can i compare two arrays of different length and keys on the basis of matching key values?

I have tried this, but it is giving the correct result我试过这个,但它给出了正确的结果

Array1数组1

Array1: [
  {
    id: 2,
    city: 'washington',
    code: 0099,
    room: 5,
    ...
  },
  {
   ...
  },
  ...
]

Array 2阵列 2

 Array2: [
      {
        "id": 2,
        "name": "john"
        "number": 727625,
        "etage": 5,
        "status": 0,
        ...
      },
      {
       ...
      },
      ...
    ]

My Code我的代码

 let Result = [];
           if (Array1 && Array1.length > 0 && Array2 && Array2.length > 0) {       
               Array1.forEach((arr1, index) => {
                 Array2.forEach((arr2, index) =>{              
                   if (arr1.id === arr2.id && arr1.room === arr2.etage) {
                      Result.push(arr1)
                   }
                 })
               })
           }
        console.log(Result)

What I want ?我想要的是 ?

I want items(objects) of Array1 by comparing both arrays, where both have same id's && room from Array1's object equal to the etage from Array2's object.我想要通过比较两个数组来获得Array1的项目(对象),其中两个数组的 id's && 来自Array1 的对象的房间等于来自Array2 的对象的etage

Please guide me, how can I do this in ES6 style in React js?请指导我,如何在 React js 中以 ES6 样式执行此操作?

The main problem with nested loops is the unnecessary iteration of each element of the first array and multiple iterations of the second array.嵌套循环的主要问题是第一个数组的每个元素的不必要迭代和第二个数组的多次迭代。

This approach takes two loops, one for generating all keys from array2 and the other to filter array1 .这种方法需要两个循环,一个用于从array2生成所有键,另一个用于过滤array1

You could take a Set for compound key of id and etage and filte the array for getting the items with same id and room .您可以为idetage复合键Set一个Set并过滤数组以获取具有相同idroom的项目。

const
    getKey = (...values) => values.join('|'),
    keys = new Set(array2.map(({ id, etage }) => getKey(id, etage))),
    result = array1.filter(({ id, room }) => keys.has(getKey(id, room));

With condition有条件

room > etage

and a Map .Map

const
    ids = array2.reduce(
        (m, { id, etage }) => m.set(id, Math.min(etage, m.get(id) || 0)),
        new Map
    ),
    result = array1.filter(({ id, room }) => room > ids.get(id));

I'd do something like this:我会做这样的事情:

 Array1= [ { id: 2, city: 'washington', code: 0099, room: 5, } ]; Array2= [ { "id": 2, "name": "john", "number": 727625, "etage": 5, }, ]; const result = Array1.filter(a1 => Array2.find(a2 => a1.id == a2.id) && Array2.find(a2 => a1.room == a2.etage)); console.log(result);

That will return a filtered array by room, etage and id.这将返回一个按房间、etage 和 id 过滤的数组。

You can use filter and some ES6 methods:您可以使用filtersome ES6 方法:

 const arr1 = [ { id: 1, room: 1 }, { id: 2, room: 5 }, { id: 3, room: 3 } ]; const arr2 = [ { id: 0, etage: 0 }, { id: 2, etage: 5 }, { id: 3, etage: 3 } ]; const getTheSame = (arr1, arr2) => { return arr1.filter(o1 => arr2.some(o2 => o1.id === o2.id && o1.room === o2.etage) ); }; console.log("Result: ", getTheSame(arr1, arr2));

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

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