--- title: Array.prototype.some() slug: Web/JavaScript/Reference/Global_Objects/Array/some tags: - ECMAScript 5 - JavaScript - Mảng - Phương Thức - Prototype - Tham khảo translation_of: Web/JavaScript/Reference/Global_Objects/Array/some ---
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.
arr.some(callback(element[, index[, array]])[, thisArg])
callback
element
index
{{Optional_inline}}array
{{Optional_inline}}some()
ở trên.thisArg
{{Optional_inline}}this
khi thực thi hàm callback
.true
khi hàm callback
trả về một giá trị {{Glossary("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
.
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ị {{jsxref("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ụ 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
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
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
var fruits = ['apple', 'banana', 'mango', 'guava']; function checkAvailability(arr, val) { return arr.some(arrVal => val === arrVal); } checkAvailability(fruits, 'kela'); // false checkAvailability(fruits, 'banana'); // true
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
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 {{jsxref("Object")}} and {{jsxref("TypeError")}} have their original values and that fun.call
evaluates to the original value of {{jsxref("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ả | Trạng thái | Chú thích |
---|---|---|
{{SpecName('ES5.1', '#sec-15.4.4.17', 'Array.prototype.some')}} | {{Spec2('ES5.1')}} | Được đưa vào lần đầu trong JavaScript 1.6. |
{{SpecName('ES6', '#sec-array.prototype.some', 'Array.prototype.some')}} | {{Spec2('ES6')}} | |
{{SpecName('ESDraft', '#sec-array.prototype.some', 'Array.prototype.some')}} | {{Spec2('ESDraft')}} |
{{Compat("javascript.builtins.Array.some")}}