0

I have an array of objects:

arr1 = [
        {
            "id": 1,
            "color": "blue",
            "label": "School",
        },
        {
            "id": 2,
            "color": "red",
            "label": "Work",
        }
    ]

and a simple array:

arr2 = [ 2, 5 ]

I want to write a method that returns true if one of the object ids from arr1 can be found in arr2. So I could use it with

v-if

later. What's your suggestion?

2
  • 2
    arr1.some(({ id }) => arr2.includes(id)) Commented Sep 20, 2022 at 14:10
  • What have you tried so far, Commented Sep 20, 2022 at 14:18

1 Answer 1

1
let arr1 = [
  {
      "id": 1,
      "color": "blue",
      "label": "School",
  },
  {
      "id": 2,
      "color": "red",
      "label": "Work",
  }
]

let arr2 = [ 2, 5 ]

let arr1_id = arr1.map(function (obj) {
  return obj.id;
});

function check(array1, array2) {
  let intersection = array1.filter(element => array2.includes(element));
  if (intersection.length > 0) {
    return true
  }
  else {return false}
}

bool = check(arr1_id,arr2)
console.log(bool)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.