Check if any values are over 18:
const ages = [3, 10, 18, 20];
ages.some(checkAdult);
function checkAdult(age) {
return age > 18;
}
Try it Yourself »
The some()
method checks if any array elements pass a test (provided as a callback function).
The some()
method executes the callback function once for each array element.
The some()
method returns true
(and stops) if the function returns true
for one of the array elements.
The some()
method returns false
if the function returns false
for all of the array elements.
The some()
method does not execute the function for empty array elements.
The some()
method does not change the original array.
array.some(
function(value, index, arr), this)
Parameter | Description | ||||||
function | Required. A function to run for each array element. |
||||||
Function parameters:
|
|||||||
this | Optional. Default undefined. A value passed to the function to be used as its "this" value. |
Type | Description |
A boolean | true if any of the aray elements pass the test, otherwise false . |
<p>Input: <input type="number" id="toCheck" value="15"></p>
<button onclick="myFunction()">Test</button>
<p>Values higher: <span id="demo"></span></p>
<script>
const numbers = [4, 12, 16, 20];
function checkValue(x) {
return x > document.getElementById("toCheck").value;
}
function myFunction() {
document.getElementById("demo").innerHTML = numbers.some(checkValue);
}
</script>
Try it Yourself »
some()
is an ECMAScript3 (ES3) feature.
ES3 (JavaScript 1999) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!