JavaScript Array method: some()
[this page | pdf | back links]
The some() method (when applied to a JavaScript
array) returns
true if at least one element
passes a specified test, otherwise returns false.
It only executes until the function it uses returns a true value and then returns true. It does not execute the function for array elements
without values. It does not change the original array
It
has the following syntax with the following parameters. It returns a
Boolean as above.
array.some(function(currentValue, index,
arr), thisValue)
Parameter
|
Required / Optional
|
Description
|
function(currentValue,
index, arr)
|
Required
|
A function to be run
for each element
|
thisValue
|
Optional
|
A value to be passed to
the function to be used as its ‘this’ value (if empty then thisValue
will be undefined)
|
The function
arguments are:
Parameter
|
Required / Optional
|
Description
|
currentValue
|
Required
|
The value of the
current element
|
Index
|
Optional
|
The array index of the
current element
|
arr
|
Optional
|
The array object which
the current element belongs to
|
EXAMPLE:
HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>
<script>
var h = [-1, 2, 3];
document.getElementById("Example").innerHTML =
'var h = [-1, 2, 3];<br>' +
'function positive(x) {<br>' +
' return x > 0;<br>' +
'}<br>' +
'var x = h.some(positive);';
document.getElementById("Result").innerHTML
= h.some(positive);
function positive(x) {
return x > 0;
}
</script>
</body>
</html>
|
FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodArraySome() {
var h = [1, 2, 3]; return !!h.some;
} |
NAVIGATION LINKS
Contents | Prev | Next | JavaScript Arrays