Array.prototype.some()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Array
实例的 some()
方法测试数组中至少一个元素是否通过所提供函数实现的测试。如果在数组中找到所提供的函数返回 true 的元素,则返回 true;否则返回 false。它不会修改数组。
¥The some()
method of Array
instances tests whether
at least one element in the array passes the test implemented by the provided
function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.
Try it
语法
参数
返回值
描述
¥Description
some()
方法是 迭代法 方法。它为数组中的每个元素调用一次提供的 callbackFn
函数,直到 callbackFn
返回 truthy 值。如果找到这样的元素,some()
立即返回 true
并停止遍历数组。否则,如果 callbackFn
对所有元素返回 falsy 值,则 some()
返回 false
。请阅读 迭代法 部分,了解有关这些方法一般如何工作的更多信息。
¥The some()
method is an iterative method. It calls a provided callbackFn
function once for each element in an array, until the callbackFn
returns a truthy value. If such an element is found, some()
immediately returns true
and stops iterating through the array. Otherwise, if callbackFn
returns a falsy value for all elements, some()
returns false
. Read the iterative methods section for more information about how these methods work in general.
some()
的作用类似于数学中的 "那里存在" 量词。特别是,对于空数组,无论任何条件它都会返回 false
。
¥some()
acts like the "there exists" quantifier in mathematics. In particular, for an empty array, it returns false
for any condition.
callbackFn
仅针对已赋值的数组索引调用。稀疏数组 中的空槽不会调用它。
¥callbackFn
is invoked only for array indexes which have assigned values. It is not invoked for empty slots in sparse arrays.
some()
不会改变调用它的数组,但 callbackFn
提供的函数可以。但请注意,数组的长度是在第一次调用 callbackFn
之前保存的。所以:
¥some()
does not mutate the array on which it is called, but the function provided as callbackFn
can. Note, however, that the length of the array is saved before the first invocation of callbackFn
. Therefore:
- 当对
some()
的调用开始时,callbackFn
将不会访问任何超出数组初始长度添加的元素。 - 对已访问索引的更改不会导致再次对它们调用
callbackFn
。 - 如果数组中现有的、尚未访问的元素被
callbackFn
更改,则传递给callbackFn
的值将是该元素被访问时的值。已删除 个元素未被访问。
警告:上述类型的并发修改经常会导致代码难以理解,通常应该避免(特殊情况除外)。
¥Warning: Concurrent modifications of the kind described above frequently lead to hard-to-understand code and are generally to be avoided (except in special cases).
some()
方法是 generic。它只期望 this
值具有 length
属性和整数键控属性。
¥The some()
method is generic. It only expects the this
value to have a length
property and integer-keyed properties.
示例
测试数组元素的值
¥Testing value of array elements
下面的示例测试数组中是否有任何元素大于 10。
¥The following example tests whether any element in the array is bigger than 10.
function isBiggerThan10(element, index, array) {
return element > 10;
}
[2, 5, 8, 1, 4].some(isBiggerThan10); // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true
使用箭头函数测试数组元素
¥Testing array elements using arrow functions
箭头函数 为相同的测试提供了更短的语法。
¥Arrow functions provide a shorter syntax for the same test.
[2, 5, 8, 1, 4].some((x) => x > 10); // false
[12, 5, 8, 1, 4].some((x) => x > 10); // true
检查数组中是否存在某个值
¥Checking whether a value exists in an array
为了模仿 includes()
方法的功能,如果该元素存在于数组中,此自定义函数将返回 true
:
¥To mimic the function of the includes()
method, this custom function returns true
if the element exists in the array:
const fruits = ["apple", "banana", "mango", "guava"];
function checkAvailability(arr, val) {
return arr.some((arrVal) => val === arrVal);
}
checkAvailability(fruits, "kela"); // false
checkAvailability(fruits, "banana"); // true
将任何值转换为布尔值
¥Converting any value to Boolean
const TRUTHY_VALUES = [true, "true", 1];
function getBoolean(value) {
if (typeof value === "string") {
value = value.toLowerCase().trim();
}
return TRUTHY_VALUES.some((t) => t === value);
}
getBoolean(false); // false
getBoolean("false"); // false
getBoolean(1); // true
getBoolean("true"); // true
使用 callbackFn 的第三个参数
¥Using the third argument of callbackFn
如果你想访问数组中的另一个元素,特别是当你没有引用该数组的现有变量时,array
参数非常有用。以下示例首先使用 filter()
提取正值,然后使用 some()
检查数组是否严格递增。
¥The array
argument is useful if you want to access another element in the array, especially when you don't have an existing variable that refers to the array. The following example first uses filter()
to extract the positive values and then uses some()
to check whether the array is strictly increasing.
const numbers = [3, -1, 1, 4, 1, 5];
const isIncreasing = !numbers
.filter((num) => num > 0)
.some((num, idx, arr) => {
// Without the arr argument, there's no way to easily access the
// intermediate array without saving it to a variable.
if (idx === 0) return false;
return num <= arr[idx - 1];
});
console.log(isIncreasing); // false
在稀疏数组上使用 some()
对非数组对象调用 some()
¥Calling some() on non-array objects
some()
方法读取 this
的 length
属性,然后访问其键为小于 length
的非负整数的每个属性,直到它们全部被访问或 callbackFn
返回 true
。
¥The some()
method reads the length
property of this
and then accesses each property whose key is a nonnegative integer less than length
until they all have been accessed or callbackFn
returns true
.
const arrayLike = {
length: 3,
0: "a",
1: "b",
2: "c",
3: 3, // ignored by some() since length is 3
};
console.log(Array.prototype.some.call(arrayLike, (x) => typeof x === "number"));
// false
规范
Specification |
---|
ECMAScript Language Specification # sec-array.prototype.some |
浏览器兼容性
BCD tables only load in the browser