English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Array some() Method

 JavaScript Array Object

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.

Syntax:

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‹/›

Browser compatibility

The numbers in the table specify the first browser version that fully supports the some() method:

Method
some()is1.5isis9

Parameter value

ParameterDescription
callback
The function that is run for each element in the array.
Function parameters:
  • element(Required)-the current element being processed in the array

  • index(Optional)-the index of the current element being processed in the array

  • array(Optional)- calledsome()array

thisArg(Optional) executecallbackis used asThisValue

Technical details

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); // true
Test and see‹/›

 JavaScript Array Object