Latest web development tutorials

JavaScript Array some () method

Array Object Reference JavaScript Array Object

Examples

Detecting whether the array element is greater than 18:

var ages = [3, 10, 18, 20];

function checkAdult (age) {
return age> = 18;
}

function myFunction () {
document.getElementById ( "demo") .innerHTML = ages.some (checkAdult);
}

The output is:

true

try it"

Definition and Usage

some () method for the detection of elements in the array if a specified condition (function provided) meet.

some () method will be executed sequentially for each element of the array:

  • If there is one element satisfies the condition, the expression returns true, the remaining elements will no longer perform detection.
  • If you do not meet the conditions of the elements, it returns false.

Note: some () does not detect an empty array.

Note: some () does not change the original array.


Browser Support

Figures in the table represent the first browser to support the method version number.

method
some () Yes 9 1.5 Yes Yes

grammar

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

Parameter Description

parameter description
function (currentValue, index, arr) have to. Function, each element of the array will perform this function function parameters:
parameter description
currentValue have to. The value of the current element
index Optional. The index value of the current element
arr Optional. An array of objects belonging to the current element
thisValue Optional. As the callback object use, passed to the function, it is used as "this" value.
If you omit thisValue, "this" value "undefined"

technical details

return value: Boolean value. If there are elements in the array satisfies the condition returns true, otherwise returns false.
JavaScript version: 1.6

More examples

Examples

Detecting whether there are elements in the array ages greater than the value of the input box:

<p> Minimum age: <input type = "number" id = "ageToCheck" value = "18"> </ p>
<button onclick = "myFunction () "> point I </ button>

<p> Analyzing the results: <span id = "demo" > </ span> </ p>

<Script>
var ages = [4, 12, 16, 20];

function checkAdult (age) {
return age> = document.getElementById ( "ageToCheck ") .value;
}

function myFunction () {
document.getElementById ( "demo") .innerHTML = ages.some (checkAdult);
}
</ Script>

try it"

Array Object Reference JavaScript Array Object