0

I have 2 arrays.

array1 = [13, 15,18,19, 25]
array2 = [{id:13, label: 'value1'},{id:15,label:'value2'},{id:25, label:'value3'}]

How to check if all the values of array2.id is present in array1?

3
  • There is nothing reactjs related in this question Commented Jun 18, 2021 at 9:05
  • 1
    const checker = (arr, target) => target.every(({id}) => arr.includes(id)); console.log(checker(array1,array2)) Commented Jun 18, 2021 at 9:11
  • Thanks @mplungjan. You are life saver!! Commented Jun 18, 2021 at 13:17

1 Answer 1

0

Iterate over array 2 and check that the id from every element is included in array 1.

array2.every(({ id }) => array1.includes(id));

const array1 = [13, 15, 18, 19, 25];
const array2 = [{
  id: 13,
  label: 'value1'
}, {
  id: 15,
  label: 'value2'
}, {
  id: 25,
  label: 'value3'
}];

const res = array2.every(({ id }) => array1.includes(id));

console.log(res);

Sign up to request clarification or add additional context in comments.

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.