Array.prototype.some()
some() 方法测试数组中的某些元素是否通过由提供的函数实现的测试,返回true或false。
只要有一个值通过测试返回了true,some方法就返回true,否则,返回false。
callback
用来测试每个元素的函数
thisArg可选
执行 callback 时使用的 this 值。
some 为数组中的每一个元素执行一次 callback 函数,直到找到一个使得 callback 返回一个“真值”(即可转换为布尔值 true 的值)。如果找到了这样一个值,some 将会立即返回 true。否则,some 返回 false。callback 只会在那些”有值“的索引上被调用,不会在那些被删除或从来未被赋值的索引上调用。
callback 被调用时传入三个参数:元素的值,元素的索引,被遍历的数组。
如果为 some 提供了一个 thisArg 参数,将会把它传给被调用的 callback,作为 this 值。否则,在非严格模式下将会是全局对象,严格模式下是 undefined。
some 被调用时不会改变数组。
some 遍历的元素的范围在第一次调用 callback. 时就已经确定了。在调用 some 后被添加到数组中的值不会被 callback 访问到。如果数组中存在且还未被访问到的元素被 callback 改变了,则其传递给 callback 的值是 some 访问到它那一刻的值。
空数组返回false。(空数组中所有元素都不符合给定的条件,注:因为空数组没有元素)。
function isBiggerThan10(element, index, array) { return element > 10; } [2, 5, 8, 1, 4].some(isBiggerThan10); // false [12, 5, 8, 1, 4].some(isBiggerThan10); // true
判断数组元素中是否存在某个值
var fruits = ['apple', 'banana', 'mango', 'guava']; function checkAvailability(arr, val) { return arr.some(function(arrVal) { return val === arrVal; }); } checkAvailability(fruits, 'kela'); // false checkAvailability(fruits, 'banana'); // true
polyfill
在第 5 版时,some 被添加进 ECMA-262 标准;这样导致某些实现环境可能不支持它。你可以把下面的代码插入到脚本的开头来解决此问题,从而允许在那些没有原生支持它的实现环境中使用它。该算法是 ECMA-262 第 5 版中指定的算法,假定 Object 和 TypeError 拥有他们的初始值,且 fun.call 等价于 Function.prototype.call。
// Production steps of ECMA-262, Edition 5, 15.4.4.17 // Reference: http://es5.github.io/#x15.4.4.17 if (!Array.prototype.some) { Array.prototype.some = function(fun/*, thisArg*/) { 'use strict'; if (this == null) {//空数组抛出错误 throw new TypeError('Array.prototype.some called on null or undefined'); } if (typeof fun !== 'function') {//fun不是函数抛出错误 throw new TypeError(); } var t = Object(this);//转换成对象 var len = t.length >>> 0;//数组的长度 var thisArg = arguments.length >= 2 ? arguments[1] : void 0;//第二个参数this for (var i = 0; i < len; i++) { if (i in t && fun.call(thisArg, t[i], i, t)) {//如果有一个返回true,就返回true return true; } } return false; }; }