Last active
August 11, 2020 08:07
-
-
Save decentraliser/e213369da5be3290bad83a786539547b to your computer and use it in GitHub Desktop.
Test for arrays intersections with array.some
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// array some helps keeping code succinct when looking for occurences in arrays | |
const firstArray = ['shark', 'fish', 'dinosaur'] | |
const secondArray = ['whale', 'shark', 'bird'] | |
const thirdArray = ['raptor', 'tuna'] | |
// determine wether 2 arrays have a similar entry | |
const bool1 = firstArray.some(animal => secondArray.some(otherAnimal => otherAnimal === animal)) | |
console.log("bool1", bool1) // true, "shark" is in both arrays | |
const bool2 = firstArray.some(animal => thirdArray.some(otherAnimal => otherAnimal === animal)) | |
console.log("bool2", bool2) // false, firstArray and thirdArray have no similar entry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment