-4

I need check if a objects array contains all elements of array, this is my objects array:

let array1 = [
    {id: 123, name: "Name of item"},
    {id: 456, name: "Other name"}
]

This is my array to find all the elements in the array1:

let array2 = [123, 456]

I want to know if all elements of array2 are in array1 in id property.

  • Iterate through array2. Use a nested loop to iterate through array1, checking if `array1[j].id == array2[i]`. If it does, break the inner loop and continue the outer loop. If you reach the end of the inner loop without breaking, it doesn't contain one of the values and you can break the outer loop. If the outer loop finishes successfully, it contains all of the values. – Liftoff Aug 17 '21 at 19:14
  • 1
    Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Aug 17 '21 at 19:17

5 Answers5

1

The question is not entirely clear, but the code below is meant to check if all the values in array2 are present as id's in array1's objects:

const arraysMatch = array2.every(array2Item => {
  return array1.find(array1Object => array1Object.id === array2Item)
})

console.log(arraysMatch)
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

Please use this code.

const id_arr = array1.map(val => val.id);
const isAllExist = array2.every(val => id_arr.indexOf(val) > -1);
console.log(isAllExist);
Kirill Savik
  • 1,228
  • 4
  • 7
0

You can use map and every for this. With map you extract the ids into an array (keys). With every you check if for every element in array2 a condition holds.

let array1 = [
    {id: 123, name: "Name of item"},
    {id: 456, name: "Other name"}
]
let array2 = [123, 456]

const keys = array1.map(el => el.id)
console.log(array2.every(id => keys.includes(id)))
A_A
  • 1,832
  • 2
  • 11
  • 17
0

You could the combination of every and indexOf to get the desired result. Check if every element of the array2 is present in the id's array that is returned from a map and then use indexOf.

const array1 = [
  { id: 123, name: "Name of item" },
  { id: 456, name: "Other name" }
];
const array2 = [123, 456];
const found = array2.every(item => array1.map(element => element.id).indexOf(item) >= 0);
console.log(found);
joy08
  • 9,004
  • 8
  • 38
  • 73
-4

Usage of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some should be able to do that for you

const array = [1, 2, 3, 4, 5];

// checks whether an element is even
const even = (element) => element % 2 === 0;

console.log(array.some(even));
// expected output: true
human
  • 33
  • 7
  • Wouldn't [every](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every) make more sense? – A_A Aug 17 '21 at 19:14