hafiz adeel
hafiz adeel

Reputation: 71

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

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

Array 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.

Please guide me, how can I do this in ES6 style in React js?

Upvotes: 0

Views: 1530

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386560

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.

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.

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.

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

Upvotes: 3

Julia
Julia

Reputation: 704

You can use filter and some ES6 methods:

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

Upvotes: 1

Jorge Zuverza
Jorge Zuverza

Reputation: 915

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.

Upvotes: 1

Related Questions