Javascript: Query an array without looping

Sararellano
3 min readJan 28, 2021

In JavaScript there’s some Array methods, like forEach(), indexOf(), map(), reduce()… But today I want to show you (if you don’t know them), three methods that seem to be less frequently used to query an array “without looping”. I put it in quotes because it is not entirely true: you don’t make a loop, but internally the loop is created.

Let’s get down to business: some(), every() and find()

  1. SOME()

This method determines if one or more values are in your array and return false or true, depending on whether the values match. But it is better to show an example:

Imagine that you have some clients and you want to send an email only to those who have pets:

const clients = [  { id: 1, name: 'Sheldon Cooper', pet: false, age: 12 },  { id: 2, name: 'Leonard Hofstadter', pet: false, age: 32 },  { id: 3, name: 'Raj Koothrappali', pet: true, age: 31 },  { id: 4, name: 'Howard Wolowitz', pet: false, age: 33 },]

To find if any client has a pet and not use a loop, you would surely use forEach()and your code will be something like this:

let hasPet = false;clients.forEach(client => {
if ( client.pet ) {
hasPet = true;
}
}) // true

But you can have the same result with less lines, using some():

let hasPet = clients.some(client => client.pet); // true

NOTE!

Some() stops iterating over the array as soon as it finds an element that matches the condition. If it finds an element that matches the condition, it automatically returns true without inspecting the remaining elements. In my example above, some() runs 3 times because “Raj Koothrappali” has a pet (true)

And, if every results return false, some() will return false.

2. EVERY()

Too very similar to some() is every(). This function is used to search too if any value matches with the condition. BUT the difference is that every() will return true only if every occurence matches. So we can say that every() search if EVERY element matches the condition.

Following the above example, I’m going to replace some() with every():

let hasPet = clients.every(client => client.pet); // false

You can use it, for instance, if you want to send an email to all clients of legal age, and your code will be something like this:

const legalAge = clients.every(client => client.age > 18);if( legalAge ) {
sendEmail()
}

And like some(), the execution of every() stops as soon as every() finds an array element that doesn’t match the condition and automatically returns false and doesn’t iterate over the remaining elements.

3. FIND()

This method does what it says: find what you are looking for in an array. It returns the first value that meets a certain condition. If there is no value that meets the condition, it returns undefined.

It’s very similar to every() and some(), the difference is that these methods return a boolean, and find() instead of returning a boolean, it will return the whole element in your array.

It’s always better to see an example. Following our clients array, we want to see if there is a person with less than 18 years.

// some()let legalAgeSome = clients.some(client => client.age < 18); 
// true
// every()let legalAgeEvery = clients.every(client => client.age < 18);
// false
// find()let legalAgeFind = clients.find(client => client.age < 18);
// {id: 1, name: "Sheldon Cooper", pet: false, age: 12}

NOTE!

Like I said, it will only return the first match, so if you need all values that match your condition you should use filter() instead of find(). For example, we want to see everybody in our array with legal age:

// filter()let legalAgeFilter = clients.filter(client => client.age > 18); 
// 0: {id: 2, name: "Leonard Hofstadter", pet: false, age: 32}
1: {id: 3, name: "Raj Koothrappali", pet: true, age: 31}
2: {id: 4, name: "Howard Wolowitz", pet: false, age: 33}

To sump up, use some(), every() and find() instead of for() or forEach() not only makes your code shorter, it also makes your code clearer, where your intentions are better seen.

--

--