0

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?

3
  • 2
    You seem to know what you want, so may you share what you have tried so far? Commented Oct 12, 2020 at 19:17
  • 3
    ust using the Array iteration methods built into JS is fine for this: Commented Oct 12, 2020 at 19:20
  • @evolutionxbox i edited my question , please check now Commented Oct 12, 2020 at 19:31

3 Answers 3

3

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)); 
Sign up to request clarification or add additional context in comments.

4 Comments

ok, please also tell about the condition => if (room > etage) ?
@hafizadeel, where is the condition room > etage coming from?
In case , if I want to make condition, where room in any of Array1's object greater than etage in any of Array2's object ?
that is a more complex problem, because you need the minimum value of etage. In this case, i would take a Map with id as key and the minimum as value.
1

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.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.