Find out if items in Array1 can be found in Array2

Find out if items in Array1 can be found in Array2

I have 2 arrays, and I wanted to find if there were some items that overlapped! This is how I did it:

var Array1 = ["cat", "dog", "fish"]
var Array2 = ["apple", "cat", "poop"]
var list = []
Array1.some(item => {
  if (Array2.includes(item)) {
    list.push(item);
  }
})
console.log(list) // ["cat"]

or

var doesItExist = (arrayA, arrayB) => {
  return arrayA.some(r=> arrayB.includes(r))
}
doesItExist(Array1, Array2) // true

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *