0

I am trying to loop a array of objects and compare it with another array and check of the key value is present in the array which is to be compared. Here I am not going to check for the array length too.

Example

Array1 is the array which needs to be compared to array 2

[{name:'Linus',id:1},{name:'Anthony',id:2},{name:'Carl',id:3}]

Array 2

[{name:'Linus',id:1},{name:'Anthony',id:2},{name:'Beth',id:3},{name:'Kyle',id:4}]

I am trying to validate if all the id values in array 1 are present in array 2 and if not present then I expect a boolean value and get the best solution in terms of performance.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

3 Answers3

0
array1.every(item1 => array2.some(item2 => item1.id === item2.id))
Alex Chuev
  • 693
  • 5
  • 17
  • Thanks for the quick solution.Gave a idea of how it can be done.Thanks <3 –  Jan 05 '23 at 11:44
0

const arr1 = [{
  name: 'Linus',
  id: 1
}, {
  name: 'Anthony',
  id: 2
}, {
  name: 'Carl',
  id: 3
}];

const arr2 = [{
  name: 'Linus',
  id: 1
}, {
  name: 'Anthony',
  id: 2
}, {
  name: 'Beth',
  id: 3
}, {
  name: 'Kyle',
  id: 4
}]

let compareTwoArrayOfObjects = (
  first_array_of_objects,
  second_array_of_objects
) => {
  return (
    first_array_of_objects.length === second_array_of_objects.length &&
    first_array_of_objects.every((element_1) =>
      second_array_of_objects.some(
        (element_2) =>
        element_1.id === element_2.id
      )
    )
  );
};

console.log(compareTwoArrayOfObjects(arr1, arr2));

You can do it with this method to compare each object's element and check their id value

Chris G
  • 1,598
  • 1
  • 6
  • 18
  • The OP never specifies that the length of the arrays would necessarily be equal. The first array could have more items than needed. – Darryl Noakes Jan 04 '23 at 15:35
  • then both arrays would no longer be equal, I'm pretty sure thats what the OP actually needs. Makes no sense having an array where a bunch of users share the same id, compare with a short array with only 3 users and return true becuz they are all present in both – Chris G Jan 04 '23 at 16:33
  • Except for the check for the length of array.The solution is fine.Thanks <3 –  Jan 05 '23 at 11:43
0

Pretty easy using the .findIndex() in a loop and filter depending upon your needs.

const array1 = [{
  name: 'Linus',
  id: 1
}, {
  name: 'Anthony',
  id: 2
}, {
  name: 'Carl',
  id: 3
}];

const array2 = [{
  name: 'Linus',
  id: 1
}, {
  name: 'Anthony',
  id: 2
}, {
  name: 'Beth',
  id: 3
}, {
  name: 'Kyle',
  id: 4
}];

array1.forEach((a1) => {
  const idx = array2.findIndex((x) => {
    return x.id === a1.id
  });
  console.log(idx);
});
// idx set to -1 if it is not found
array2.forEach((a2) => {
  const idx = array1.findIndex((x) => {
    return x.id === a2.id
  });
  console.log(idx);
});

let matches = array2.filter((a) => {
  return array1.findIndex((x) => {
    return x.id === a.id
  }) > -1;
});
console.log(matches);

let nomatches = array2.filter((a) => {
  return array1.findIndex((x) => {
    return x.id === a.id
  }) == -1;
});
console.log(nomatches);
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100