107 of 264 menu

some method

The some method checks the array elements according to a passed function. This function is passed as a method parameter and is executed for each element of the array. The method returns true if the passed function returns true for at least one array element, otherwise the method returns false.

You can pass three parameters to the function. If these parameters are present (they are not required), then the first one will automatically get the array element, the second one will get its number in the array (index), and the third one will get the array itself.

Syntax

array.some(function(element, index, array) { return true или false; });

Example

We check if there is at least one positive number in the array of numbers:

let arr = [-1, -2, -3, 4]; let check = arr.some(function(elem) { if (elem >= 0) { return true; } else { return false; } }); console.log(check);

The code execution result:

true

Example

Let's check that at least one product of an element and its index is greater than or equal to 20:

let arr = [1, 2, 3, 4, 5]; let check = arr.some(function(elem, index) { if (elem * index >= 20) { return true; } else { return false; } }); console.log(check);

The code execution result:

true

Example

If necessary, an array itself can be passed to the third parameter:

let check = arr.some(function(elem, index, arr) { array arr will be available here });

See also

  • the every method
    that also allows you to perform an array check
  • the map and forEach methods
    that allow you to apply a function to each element of an array
  • the reduce and reduceRight methods
    that reduce an array to a single value
byenru