JavaScript arrays often need to be interrogated to find if they contain elements that match certain criteria, or are above or below certain values, or if all “slots” in the array match something. While you can use the traditional approach of looping through the array and testing each individual value using a for
loop - or, in a more modern approach, using a forEach
method - there’s a better way: using every
or some
to instantly get an answer.
every
Let’s say we have an array that contains a series of ages:
var ages = [ 21, 18, 32, 45, 56 ];
If we want to check that every member of the array is an adult (18 or older, for the purpose of this example), we could write the following in the console, using arrow functions:
ages.every(age => age >= 18)
> true
The result will either be true
or false
, so it could also be written as the test in an if
statement:
if (ages.every(age => age >= 18)) {
// do something
}
Because the every
method contains a callback, we could also do the comparison work in an external function. Let’s say we have a JSON array of insects:
var insects = [
{ name: "Monarch", order: "Lepidoptera" },
{ name: "Chinese silkworm moth", order: "Lepidoptera" },
{ name: "Common Buckeye", order: "Lepidoptera" },
{ name: "Hummingbird Hawk-moth", order: "Lepidoptera" }
];
We want to check that every insect in the structure is a member of the order Lepidoptera (the family of moths and butterflies). We could write a function to do so:
function isButterflyOrMoth(element) {
return (element.order === "Lepidoptera");
}
Calling the function on the array and displaying the result with:
console.log(insects.every(isButterflyOrMoth));
Which, in this case, would return true
.
some
Similarly, we can also test that some
of the array content passes a particular criteria. Let’s say we want to check that at least one of the entries in the age
array is less than 30. The array itself is unchanged:
var ages = [ 21, 18, 32, 45, 56 ];
We want to check that at least one of the age values is less than 30:
ages.some(age => age < 30)
> true
some
stops at the first match it finds in an array. As such, it can also be used to efficiently check if an array contains a particular value. Given an array containing the original Greek muses:
var muses = ["Calliope", "Clio", "Euterpe", "Thalia", "Melpomene",
"Terpsichore", "Erato", "Polyhymnia", "Urania"];
We can check if a value is present using a function:
function isPresent(array, val) {
return array.some(arrVal => val === arrVal);
}
Using this, we can check for the presence of a value by calling the isPresent
function, passing the array and value under inspection as arguments:
isPresent(muses, "Calliope");
> true
Support
Browser support for both every
and some
is excellent: every modern browser, including IE9+, supports both methods. (Polyfills are available for IE8, if you need them).
Photograph by Agata Serge, used under a Creative Commons license.
I’d like to thank Wes Bos for inspiring this article.
Enjoy this piece? I invite you to follow me at twitter.com/dudleystorey to learn more.