some()方法检查数组中是否至少有一个元素通过了提供的函数实现的测试。
some()方法对每个数组索引执行一次回调函数:
如果找到函数通过测试的数组元素,则findIndex()立即返回true值
否则,它返回false,表明没有元素通过测试
注意: some()方法不会更改原始数组。
array.some(callback, thisArg)
var fruits = ['Banana', 'Mango', 'Apple', 'Orange'];
function hasApple(element) {
return element === 'Apple';
}
function myFunc() {
document.getElementById('result').innerHTML = fruits.some(hasApple);
}
测试看看‹/›表中的数字指定了完全支持some()方法的第一个浏览器版本:
Method | ![]() | ![]() | ![]() | ![]() | ![]() |
some() | 是 | 1.5 | 是 | 是 | 9 |
参数 | 描述 |
---|---|
callback | 为数组中的每个元素运行的函数。 函数参数:
|
thisArg | (可选)执行回调时用作此值 |
返回值: | 如果回调函数为任何数组元素返回true值,则为true;否则为假。 |
---|---|
JavaScript版本: | ECMAScript 3 |
将任何值转换为布尔值:
var arr = [true, 'true', 1];
function getBoolean(element) {
if (typeof element === '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
测试看看‹/›