The Array.some Method in JavaScript

Published: Wednesday, June 8, 2022

Greetings, friends! In this tutorial, I will discuss the Array.some method in JavaScript. If you come from the Ruby programming language, it is very similar to the Enumerable#any method in Ruby. In C#, it's similar to the Array.Exists method.

The Array.some method tests whether at least one element in an array passes a test as defined by a predicate. A predicate is a function that returns a boolean value. Let's look at an example.

javascript
Copied! ⭐️
const array = [1, 2, 3, 4, 5];

const isEven = x => x % 2 === 0;

const result = array.some(isEven);

console.log(result); // true

In the code snippet above, the isEven callback function would be considered the predicate. We are calling the predicate callback function to test whether at least one element in array is an even number or not.

Let's look at the Array.some method's documentation more closely. According to MDN, this method can accept two parameters and returns a new array with each element being the result of the callback function.

javascript
Copied! ⭐️
some(callbackFn, thisArg)

Notice that this function definition is very similar to Array.every.

The first parameter - callbackFn

The first parameter we pass into the some method is a callback function, and the second parameter is thisArg, a value to use as the this context when executing callbackFn. It's really rare to use the second parameter. It's also an optional parameter. Let's just look at the first parameter, the callback function. We'll discuss the second parameter later.

The Array.some method expects a callback function that has one, two, or three parameters. The second and third parameters are optional.

javascript
Copied! ⭐️
some((element) => { /* ... */ })
some((element, index) => { /* ... */ })
some((element, index, array) => { /* ... */ })

Using a Callback Function with One Parameter

Let's use the Array.some method with a callback function that has one parameter.

javascript
Copied! ⭐️
const people = [
  { name: 'nate', books: 100 },
  { name: 'cameron', books: 0 },
  { name: 'sam', books: 0 }
];

const atLeastOneHasBooks = people.some(person => {
  return person.books > 0;
})

console.log(atLeastOneHasBooks); // true

The code snippet above passes in an arrow function as a callback function. We could have just as easily defined our own function and pass that into the every method instead.

javascript
Copied! ⭐️
const people = [
  { name: 'nate', books: 100 },
  { name: 'cameron', books: 0 },
  { name: 'sam', books: 0 }
];

function hasBooks(people) {
  return people.books > 0;
}

const atLeastOneHasBooks = people.some(hasBooks);

console.log(atLeastOneHasBooks); // true

It's important to note that the some method will return true when it encounters the first successful test. This helps with performance, since we don't have to iterate through the entire array if it encounters a successful test early on in the array.

javascript
Copied! ⭐️
const array = [1, 2, 3, 4, 5];

const result = array.some(element => {
  console.log(element);
  return element % 2 === 0;
});

/* OUTPUT:
1
2
*/

console.log(result); // true

Using a Callback Function with Two Parameters

Let's now take a look at using the some method with a callback function that has two parameters. The second parameter will be the index of the element of the array.

javascript
Copied! ⭐️
const matrix = [[-1, 0, 0], [0, 1, 0], [0, 0, -1]];

const mainDiagonalEqualsOne = matrix.some((row, index) => {
  return row[index] > 0;
})

console.log(mainDiagonalEqualsOne); // true

In the code snippet above, we are using the some method to check if the matrix (an array of arrays in this case) has at least one value in the main diagonal that is a positive number.

The main diagonal of a matrix is where the row index is equal to the column index. In our example, the main diagonal contains either 1 or -1. The other values in the matrix are zero.

Our matrix should appear similar to the following:

text
Copied! ⭐️
[
  [-1, 0, 0],
  [0, 1, 0],
  [0, 0, -1]
]

The some method will iterate over every sub-array or row of the matrix. The following code is similar to what happens during each iteration of the some method in our example.

javascript
Copied! ⭐️
// At index = 0:
row = [-1, 0, 0];
row[0] = -1;

// At index = 1;
row = [0, 1, 0];
row[1] = 1;

// At index = 2;
row = [0, 0, 1];
row[2] = 1;

result = row[0] > 0 || row[1] > 0 || row[2] > 0;

The main diagonal of the matrix has at least one value that is greater than zero, so the test passes.

Using a Callback Function with Three Parameters

Next, let's try using a callback with three parameters. The third parameter will be the array we're calling the some method on.

javascript
Copied! ⭐️
const result = [2, 3, 4].some((num, index, thisArray) => {
  return num > thisArray.length;
});

console.log(result); // true

In the code snippet above, the parameter, thisArray, refers to a reference to the array we're calling the some method on. They are equivalent, which means that thisArray will have a length of 3. For each element in the array, we will test if at least one value is greater than the length of thisArray. Since the element, 4, is greater than the length, 3, the test passes.

You can also shorten the code snippet above by using the concise version of arrow functions.

javascript
Copied! ⭐️
const result = [2, 3, 4].some((num, index, thisArray) => num > thisArray.length);

console.log(result); // true

The second parameter - thisArg

We have now seen how to use the some method with a callback function with one, two, and three parameters. However, the callback function was only the first parameter we can pass into the some method. It also accepts a second parameter, thisArg, which is optional.

As mentioned previously, it's rare to use this second parameter. You'll typically see it when people are implementing a complex feature or trying to support old legacy code. Let's see an example of how we might use the thisArg parameter in the some method.

javascript
Copied! ⭐️
function Player() {
  this.spells = [];
  this.count = 0;
  this.canHeal = false;
}

Player.prototype.add = function (array) {
  this.spells = array;
  this.count = array.length;
  this.canHeal = array.some((element) => {
    return element.includes('cur');
  }, this);
};

const nate = new Player();
nate.add(['firaga', 'thundaga', 'blizzaga']);
console.log(nate.spells); // ['firaga', 'thundaga', 'blizzaga']
console.log(nate.count); // 3
console.log(nate.canHeal); // false

const flora = new Player();
flora.add(['cure', 'curaga', 'protect']);
console.log(flora.spells); // ['cure', 'curaga', 'protect']
console.log(flora.count); // 3
console.log(flora.canHeal); // true
warning
Arrow functions do not bind their own this context and will use their parent scope instead. Assigning an arrow function to Player.prototype.add will cause unexpected results.

In the above code snippet, we are creating a Player object by calling a function. Before JavaScript introduced the concept of classes, it was common to make objects using functions and assigning properties to them using the this keyword.

Since JavaScript is a prototypal language, we can add methods to the player's prototype. Within the Player.prototype.add method, we are calling some and passing a value of this as the second parameter. In this context, the this keyword refers to the Player prototype. This allows us to add update properties on instances of our Player object. In our example, we have two instances: nate and flora.

When we run flora.add(['cure', 'curaga', 'protect']), the player, "flora," will have each "spell" added to the list of spells. The spell count will be updated to match the total number of elements in the array. If at least one of the spells contain cur, then the canHeal property will be set to true.

Conclusion

This has been a comprehensive guide to the some method in JavaScript. In summary, this method accepts similar parameters as the every method. However, the some method returns a boolean value that will be true if at least one element in the array passes the test specified by the provided function.

The some method accepts two parameters: a callback and a value to use as the this context when executing the callback. The callback function can accept three parameters: the current element being processed, the index of that element in the array, and the array some was called upon. The callback function passed into the some method is sometimes called a predicate because it is a function that returns a boolean value.

Resources