Over a million developers have joined DZone.

Searching with Array.prototype.some

DZone's Guide to

Searching with Array.prototype.some

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

Iterating over an array to search for an item is a pretty common task. With JavaScript, Array.prototype.forEach is often the popular choice. In some cases, however, Array.prototype.some can be a better fit for the job if there is no need to search the entire array once a condition is fulfilled.

There are at least three different ways to iterate over arrays and objects in JavaScript: for loops, array methods, and listing property keys. If the iteration is being carried out to perform a search operation, it may lead to the following example:
function findEmployee(id) {
  for (var i in employees)
    if (employees[i].id === id)
      return employees[i];

Without any guards or error checks, the code above is rather self-explanatory. Now, since JavaScript Array has some powerful higher-order functions, one certainly can tweak the above code to look like this:

function findEmployee(id) {
    var employee;
    employees.forEach(function (e) {
        if (e.id === id) employee = e;
    return employee;

Let us review what the specification says about Array.prototype.forEach (Section

callbackfn should be a function that accepts three arguments. forEach calls callbackfn once for each element present in the array, in ascending order.

In our example problem, suppose that the employee’s ID is unique (ideally, of course, this would be an associative container, see also the blog post Determining Objects in a Set, but that is a different story). The search via forEach is rather inefficient. It will continue even though there is already a hit. It is mandated by the above specification, invoking the callback once for each element present.

An improvement to the search would be to stop right away once the condition is fulfilled. Note that the condition doesn't need to be only a single match. There are problems like searching the first three free spaces, locating enough suitable rooms, etc. Fortunately, there are Array methods that can do that: every and some. I have covered the use of every to implement a primality test (see the previous blog post on Prime Numbers, Factorial, and Fibonacci Series with JavaScript Array), so let's take a look at its sibling.

Section of ECMAScript 5.1 specification reveals that for Array.prototype.some:

callbackfn should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false. some calls callbackfn once for each element present in the array, in ascending order, until it finds one where callbackfn returns true.

This is exactly what we need! Rewriting the previous code fragment gives the following version. The iteration will not continue if there is a match.

function findEmployee(id) {
    var employee;
    employees.some(function (e) {
        if (e.id === id) {
            employee = e;
            return true;
    return employee;

Did you ever encounter a situation where some can be used instead of forEach? Tell us!

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


Published at DZone with permission of Ariya Hidayat, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}