How to check if a value is in an array in JavaScript
By using the JavaScript Array.prototype function includes we can quickly determine if an array contains a certain element. Here's a simple example:
const animals = ['dog', 'cat', 'cow', 'goose', 'bear'];
const containsGoose = animals.includes('goose');
console.log(containsGoose);
// true
If the value isn't in the array the method will return false like so:
const animals = ['dog', 'cat', 'cow', 'goose', 'bear'];
const containsHorse = animals.includes('horse');
console.log(containsHorse);
// false
But what if you need to check that an object inside the array contains an attribute with a given value? Well that becomes slightly more complex. Let's try it using just includes first:
const animals = [
{ name: 'dog', age: 7 },
{ name: 'cat', age: 2 },
{ name: 'cow', age: 16 },
{ name: 'goose', age: 10 },
{ name: 'bear', age: 19 },
];
const hasAge10 = animals.includes((t) => t.age === 10);
console.log(hasAge10);
// false
No bueno, Sadly we can't compare objects directly so includes won't work here. We're going to need to look for an alternative, luckily there's a very similar method that we can pass a function to some. Let's try it with that:
const animals = [
{ name: 'dog', age: 7 },
{ name: 'cat', age: 2 },
{ name: 'cow', age: 16 },
{ name: 'goose', age: 10 },
{ name: 'bear', age: 19 },
];
const hasAge10 = animals.some((t) => t.age === 10);
console.log(hasAge10);
// true
Et voila, now our code behaves as expected. It's worth noting that includes is not supported by internet explorer and you may have to either use a polyfill or fall back on some.