AppDividend
Latest Code Tutorials

JavaScript Array Some Example | Array.prototype.some()

0

Javascript array some() function tests whether some item in the array passes the test implemented by the provided function. The some() method is the JavaScript extension to the ECMA-262 standard; as such, it may not be present in other implementations of the standard.

JavaScript Array Some Example

JavaScript array some() is the inbuilt method that tests whether at least one item in the array passes the test implemented by a provided function. It returns the Boolean value.

The some() method in JavaScript executes the function once for each element present in the array:

  1. If it finds an array element where the function returns a true value, then some() function returns true (and it does not check the remaining values).
  2. Otherwise, it returns false.

Syntax

array.some(function(currentValue, index, arr), thisValue)

Parameters

JavaScript some() function takes the first parameter as a function that takes the following parameters.

  1. currentValue: It is Required. The value of the current element.
  2. index: It is Optional. The array index of the current element.
  3. arr: It is Optional. The array object the current element belongs to.

The function is the required parameter.

It takes thisValue as a second parameter, which is optional. It is the value to be passed to the function to be used as its “this” value. If the thisValue parameter is empty, the value “undefined” will be given as its “this” value.

Return Value

Javascript some() method returns true if the callback function returns a truthy value for at least one element in the array. Otherwise, it returns false.

Example

Write the following code inside the new file. Let’s call it the app.js file.

// app.js

let dark = [
  100,
  90,
  80,
];

console.log(dark.some(x => x > 90));

Output

true

In this example, we defined an array and then check if the array contains a single item that satisfies the condition, and if it does, then returns true otherwise, it returns false.

The some() function executes the callback function once for each item present in the array until it finds the one where callback returns the truthy value (a value that becomes true when converted to a Boolean).

If such an item is found, the some() method immediately returns true. Otherwise, it returns false.

The callback is invoked only for indexes of an array with assigned values. It is not invoked for indexes that have been deleted or which have never been assigned values.

The callback is invoked with three arguments: 

  1. The value of the item,
  2. The index of the item,
  3. The Array object being traversed.

If the thisArg parameter is provided to some() function then, it will be used as callback’s this value. Otherwise, the value undefined will be used as its this value.

This value ultimately observable by callback is defined according to the usual rules for determining this seen by a function.

The some() function does not mutate the array on which it is called.

The range of elements processed by the some() method is set before the first invocation of callback.

The callback will not visit items appended to the array after the call to some() begins.

If the existing, unvisited item of the array is changed by callback, its value passed to a visiting callback will be the value at a time that some() visits that item’s index. Items that are deleted are not visited.

Checking whether the value exists in an array

To check if the specified value is present in the array, use the array.some() function. Let’s see how to do that.

// app.js

const horcruxes = ['diary', 'ring', 'locket', 'nagini', 'harry'];

function remainingHorcruxes(arr, val) {
  return arr.some(function (arrVal) {
    return val === arrVal;
  });
}

console.log(remainingHorcruxes(horcruxes, 'harry'));
console.log(remainingHorcruxes(horcruxes, 'diadem'));

Output

true
false

In this example, we have defined an array, then defined a function that checks if the passed value exists in the given array. If it exists, then it will return true otherwise returns false.

Check if an array has one element that is in the range

To check if any number in the array is in the range, then use array.some() method.

// app.js

let data = [11, 18, 19, 21, 29];

const range = {
  min: 10,
  max: 30
};

let result = data.some(function (e) {
  return e >= this.min && e <= this.max;
}, range);

console.log(result);

Output

true

In this example, we are checking each element against the min and max numbers. If it satisfies the condition, then returns true otherwise false.

First, we defined a range object with min and max properties.

Second, call the some() function on the marks array object and pass the callback and range object. Because we pass a range object as the second argument (thisArg), we can reference it inside the callback via this value.

Supported Browser

The browsers supported by JavaScript Array some() method are listed below:

  1. Google Chrome
  2. Mozilla Firefox
  3. Internet Explorer
  4. Opera
  5. Safari

That is it for the Javascript array some() method.

See also

JavaScript array includes()

JavaScript array indexOf()

JavaScript array contains

JavaScript array find()

JavaScript array findIndex()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.