English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
some()The method checks if at least one element in the array passes the test implemented by the provided function.
some()The method executes the callback function once for each array index:
If the function finds an array element that passes the test, thenfindIndex()return immediatelytrueValue
otherwise, it returnsfalsemeans that no elements pass the test
Note: The some() method does not change the original array.
array.some(callback, thisArg)
var fruits = [&39;Banana', &39;Mango', &39;Apple', &39;Orange'; function hasApple(element) { return element === 'Apple'; } function myFunc() { document.getElementById('result').innerHTML = fruits.some(hasApple); }Test and see‹/›
The numbers in the table specify the first browser version that fully supports the some() method:
Method | |||||
some() | is | 1.5 | is | is | 9 |
Parameter | Description |
---|---|
callback | The function that is run for each element in the array. Function parameters:
|
thisArg | (Optional) executecallbackis used asThisValue |
Return value: | If the callback function returns a true value for any array element, it is true; otherwise, it is false. |
---|---|
JavaScript版本: | ECMAScript 3 |
将任何值转换为布尔值:
var arr = [true, 'true', 1 function getBoolean(element) { if (typeof element ===39;string'}); element = element.toLowerCase().trim(); } return arr.some(function(t) { return t === element; }); } getBoolean(false); // false getBoolean('false'); // false getBoolean(0); // false getBoolean(true); // true getBoolean('true');// true getBoolean(1); // trueTest and see‹/›