Phương thức some()
kiểm tra xem có ít nhất một phần tử của mảng thoả điều kiện ở hàm được truyền vào hay không. Kết quả trả về có kiểu Boolean
.
Chú ý: Phương thức này sẽ trả về false
nếu mảng rỗng.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Cú pháp
arr.some(callback(element[, index[, array]])[, thisArg])
Các tham số
callback
- Hàm dùng để kiểm tra từng phần tử, nhận vào ba đối số:
element
- Phần tử đang được kiểm tra.
index
Optional- Chỉ mục của phần tử đang được kiểm tra.
array
Optional- Là bản thân mảng đã gọi phương thức
some()
ở trên.
thisArg
Optional- Được sử dụng làm giá trị
this
khi thực thi hàmcallback
.
Giá trị trả về
true
khi hàm callback
trả về một giá trị truthy nếu có ít nhất một phần tử của mảng thoả điều kiện. Ngược lại sẽ trả về false
.
Mô tả
Phương thức some()
thực thi hàm callback
một lần và lặp qua từng phần tử của mảng cho đến khi hàm callback
trả về một giá trị truthy (tức là true
khi được chuyển sang kiểu Boolean). Nếu như có một phần tử thoả mãn, some()
sẽ lập tức trả về true
. Ngược lại sẽ trả về
false
. callback
được gọi chỉ khi các phần tử của mảng có giá trị.
callback
được gọi với ba đối số: giá trị của phần tử, số chỉ mục của phần tử và mảng đang được lặp qua.
Nếu như tham số thisArg
được truyền vào some()
, nó sẽ được sử dụng làm giá trị this
của callback
. Nếu bỏ qua, this
sẽ có giá trị undefined
. The this
value ultimately observable by callback
is determined according to the usual rules for determining the this
seen by a function.
some()
không làm thay đổi mảng ban đầu.
The range of elements processed by some()
is set before the first invocation of callback
. Elements appended to the array after the call to some()
begins will not be visited by callback
. If an existing, unvisited element of the array is changed by callback
, its value passed to the visiting callback
will be the value at the time that some()
visits that element's index. Elements that are deleted are not visited.
Ví dụ
Kiểm tra giá trị của các phần tử
Ví dụ bên dưới đang kiểm tra xem có phần tử nào lớn hơn 10 hay không.
function isBiggerThan10(element, index, array) {
return element > 10;
}
[2, 5, 8, 1, 4].some(isBiggerThan10); // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true
Kiểm tra giá trị của các phần tử sử dụng arrow function
Arrow functions làm cho cú pháp trở nên gọn hơn.
[2, 5, 8, 1, 4].some(x => x > 10); // false
[12, 5, 8, 1, 4].some(x => x > 10); // true
Kiểm tra phần tử có tồn tại trong mảng hay không
Hàm checkAvailability()
bên dưới đang mô phỏng lại phương thức includes()
, trả về true
nếu phần tử có tồn tại trong mảng:
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
Kiểm tra phần tử có tồn tại trong mảng hay không sử dụng arrow function
var fruits = ['apple', 'banana', 'mango', 'guava'];
function checkAvailability(arr, val) {
return arr.some(arrVal => val === arrVal);
}
checkAvailability(fruits, 'kela'); // false
checkAvailability(fruits, 'banana'); // true
Chuyển giá trị bất kì sang kiểu Boolean
var TRUTHY_VALUES = [true, 'true', 1];
function getBoolean(value) {
'use strict';
if (typeof value === 'string') {
value = value.toLowerCase().trim();
}
return TRUTHY_VALUES.some(function(t) {
return t === value;
});
}
getBoolean(false); // false
getBoolean('false'); // false
getBoolean(1); // true
getBoolean('true'); // true
Polyfill
some()
was added to the ECMA-262 standard in the 5th edition, and it may not be present in all implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of some()
in implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming Object
and TypeError
have their original values and that fun.call
evaluates to the original value of 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') {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
for (var i = 0; i < len; i++) {
if (i in t && fun.call(thisArg, t[i], i, t)) {
return true;
}
}
return false;
};
}
Đặc tả
Đặc tả | Trạng thái | Chú thích |
---|---|---|
ECMAScript 5.1 (ECMA-262) The definition of 'Array.prototype.some' in that specification. |
Standard | Được đưa vào lần đầu trong JavaScript 1.6. |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.prototype.some' in that specification. |
Standard | |
ECMAScript (ECMA-262) The definition of 'Array.prototype.some' in that specification. |
Living Standard |
Khả năng tương thích của trình duyệt
BCD tables only load in the browser