From 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:43:23 -0500 Subject: initial commit --- .../global_objects/array/includes/index.html | 135 +++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 files/vi/web/javascript/reference/global_objects/array/includes/index.html (limited to 'files/vi/web/javascript/reference/global_objects/array/includes') diff --git a/files/vi/web/javascript/reference/global_objects/array/includes/index.html b/files/vi/web/javascript/reference/global_objects/array/includes/index.html new file mode 100644 index 0000000000..bc4b1980f4 --- /dev/null +++ b/files/vi/web/javascript/reference/global_objects/array/includes/index.html @@ -0,0 +1,135 @@ +--- +title: Array.prototype.includes() +slug: Web/JavaScript/Reference/Global_Objects/Array/includes +tags: + - JavaScript + - Mảng + - Phương Thức + - Pollyfill + - Prototype + - Tham khảo +translation_of: Web/JavaScript/Reference/Global_Objects/Array/includes +--- +
{{JSRef}}
+ +

Phương thức includes() kiểm tra xem phần tử đã cho có tồn tại trong mảng hay không, trả về kết quả true hoặc false.

+ +
{{EmbedInteractiveExample("pages/js/array-includes.html")}}
+ + + +

Cú pháp

+ +
arr.includes(valueToFind[, fromIndex])
+
+ +

Các tham số

+ +
+
valueToFind
+
+

Giá trị muốn kiểm tra.

+ +
+

Lưu ý: Khi kiểm tra chuỗi hoặc kí tự, includes() sẽ phân biệt hoa thường.

+
+
+
fromIndex {{optional_inline}}
+
Vị trí trong mảng để bắt đầu tìm kiếm valueToFind; đầu tìm kiếm tại fromIndex khi fromIndex mang giá trị dương, hoặc tại array.length + fromIndex khi fromIndex  mang giá trị âm (sử dụng {{interwiki("wikipedia", "absolute value", "giá trị tuyệt đối")}} của fromIndex làm số lượng kí tự tính từ cuối mảng làm vị trí bắt đầu). Giá trị mặc định là 0.
+
+ +

Giá trị trả về

+ +

Có kiểu {{jsxref("Boolean")}}, trả về true nếu valueToFind được tìm thấy trong mảng (hoặc một phần của mảng được xác định bởi fromIndex nếu có). Các giá trị "không" được coi là bằng nhau (-0 sẽ bằng 0 và +0), nhưng false thì không bằng 0.

+ +
+

Lưu ý: Về mặt kĩ thuật, includes() sử dụng thuật toán sameValueZero để kiểm tra phần tử đã cho có tìm thấy hay không.

+
+ +

Ví dụ

+ +
[1, 2, 3].includes(2);     // true
+[1, 2, 3].includes(4);     // false
+[1, 2, 3].includes(3, 3);  // false
+[1, 2, 3].includes(3, -1); // true
+[1, 2, NaN].includes(NaN); // true
+
+ +

fromIndex lớn hơn hoặc bằng độ dài mảng

+ +

Nếu fromIndex lớn hơn hoặc bằng độ dài mảng, trả về kết quả false

+ +
var arr = ['a', 'b', 'c'];
+
+arr.includes('c', 3);   // false
+arr.includes('c', 100); // false
+ +

Computed index nhỏ hơn 0

+ +

Nếu fromIndex là số âm, computed index sẽ được dùng làm vị trí bắt đầu để tìm kiếm valueToFind. Nếu computed index nhỏ hơn hoặc bằng -1 * array.length, phần tử đã cho sẽ được tìm kiếm trong toàn bộ mảng (tương tự như fromIndex bằng 0).

+ +
// độ dài mảng là 3
+// fromIndex là -100
+// computed index là 3 + (-100) = -97
+
+var arr = ['a', 'b', 'c'];
+
+arr.includes('a', -100); // true
+arr.includes('b', -100); // true
+arr.includes('c', -100); // true
+arr.includes('a', -2); // false
+ +

includes() used as a generic method

+ +

includes() method is intentionally generic. It does not require this value to be an Array object, so it can be applied to other kinds of objects (e.g. array-like objects). The example below illustrates includes() method called on the function's arguments object.

+ +
(function() {
+  console.log([].includes.call(arguments, 'a')); // true
+  console.log([].includes.call(arguments, 'd')); // false
+})('a','b','c');
+ + + +

Đặc tả

+ + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-array.prototype.includes', 'Array.prototype.includes')}}{{Spec2('ESDraft')}}
{{SpecName('ES7', '#sec-array.prototype.includes', 'Array.prototype.includes')}}{{Spec2('ES7')}}Initial definition.
+ +

Khả năng tương thích của trình duyệt

+ +
+ + +

{{Compat("javascript.builtins.Array.includes")}}

+
+ +

Xem thêm

+ + -- cgit v1.2.3-54-g00ecf