Home > Software engineering >  compare two array of objects and perform if condition using foreach
compare two array of objects and perform if condition using foreach

Time:12-22

I am trying to compare two arrays of objects, after comparing I need to perform some conditions like if the item id matches in both the array.

After this I need to return nothing if it is false I need to return some value.

I am trying to achieve this by using foreach. The reason for using foreach is that I need to apply the condition and I need to return something.

I need to check each value in both arrays to apply the condition.

I tried with some map methods but some are returning a boolean and I couldn't apply the condition by using that. I got partial output by using foreach but looping the second array returns undefined.

I need to compare these two arrays and I need to find the items which have the same ID for these items I need to return null, for the items which are having different IDs I need to return some value. Hence I need to compare each element in the array of objects and need to apply the condition

Could anyone let me know what am I missing? Thanks in advance!

var result1 = [
    {id:1, name:'Sandra', type:'user', username:'sandra'},
    {id:2, name:'John', type:'admin', username:'johnny2'},
    {id:3, name:'Peter', type:'user', username:'pete'},
    {id:4, name:'Bobby', type:'user', username:'be_bob'}
];

var result2 = [
    {id:2, name:'John', email:'[email protected]'},
    {id:4, name:'Bobby', email:'[email protected]'}
];

result1.forEach((num1, index) => {

  // console.log(num1.id) //result coming as expected

  const num2 = result2[index];
  console.log("num2", num2.id)

  // console.log(num1.id, num2.id);//here I need to compare result 1 ID and result2 id of each element

});

CodePudding user response:

You were probably getting an error because while you're iterating through result, you'll eventually get to index === 2, and then const num2 = result2[index] is undefined, because result2 doesn't have that many elements, and anything like num2.id will throw an error.

I'm not totally sure what you're trying to achieve, but it sounds like you want to iterate through the first array, and find a matching element in the second array, then do something with them if there is a match, otherwise do nothing.

var result1 = [
    {id:1, name:'Sandra', type:'user', username:'sandra'},
    {id:2, name:'John', type:'admin', username:'johnny2'},
    {id:3, name:'Peter', type:'user', username:'pete'},
    {id:4, name:'Bobby', type:'user', username:'be_bob'}
];

var result2 = [
    {id:2, name:'John', email:'[email protected]'},
    {id:4, name:'Bobby', email:'[email protected]'}
];

const doSomethingWithMatches = (arr1, arr2) => {
  // Loop through each item in the first array
  arr1.forEach(item => {
    // Find an item in the second array with the same id
    const matchingItem = arr2.find(item2 => item2.id === item.id);

    // If there is a match, do something with them
    if (matchingItem) {
      console.log(item, matchingItem);
    }
  });
};

doSomethingWithMatches(result1, result2);

CodePudding user response:

Not sure that you really have to use Array#forEach. Depending on what you need to return you can use other methods such as Array#find as in the demo below and return the result1 element or a property of it or null.

const result1 = [
        {id:1, name:'Sandra', type:'user', username:'sandra'},
        {id:2, name:'John', type:'admin', username:'johnny2'},
        {id:3, name:'Peter', type:'user', username:'pete'},
        {id:4, name:'Bobby', type:'user', username:'be_bob'}
    ],

    result2 = [
        {id:2, name:'John', email:'[email protected]'},
        {id:4, name:'Bobby', email:'[email protected]'}
    ],
    
    output = result1
        .map(({id}) => [id, result2.find(o => o.id === id) || null]);
        
console.log( output );

CodePudding user response:

you can filter by id, and same name. Then change the value.

const mergedResults = result1.map((result1Item) => {
const result2Item = result2.find((item) => item.id === result1Item.id);
  return {
    ...result1Item,
    ...result2Item,
  };

output like this:

[
  { id: 1, name: 'Sandra', type: 'user', username: 'sandra' },
  {
    id: 2,
    name: 'John',
    type: 'admin',
    username: 'johnny2',
    email: '[email protected]'
  },
  { id: 3, name: 'Peter', type: 'user', username: 'pete' },
  {
    id: 4,
    name: 'Bobby',
    type: 'user',
    username: 'be_bob',
    email: '[email protected]'
  }
]
  • Related