Skip to content
Advertisement

Check if array of objects contains all the values of an array of integers

Is there a way to know if all the values of an array of integers are in an array of objects? If the values of the array of integers are all in the array of objects, as return I intend true and false the opposite, that is, the values are not all present in the object.

DEMO

 Sizes = [
    {
      id: 1,
    },
    {
      id: 2,
    },
    {
      id: 3,
    },
    {
      id: 4,
    },
  ];

  Number = [1, 2, 3];

Advertisement

Answer

Use every + some:

Number.every(n => Sizes.some(obj => obj.id === n))

 Sizes = [
    {
      id: 1,
    },
    {
      id: 2,
    },
    {
      id: 3,
    },
    {
      id: 4,
    },
  ];

  Number = [1, 2, 3];
  
console.log(Number.every(n => Sizes.some(obj => obj.id === n)))
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement