1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
---
<div>{{JSRef}}</div>
<p>Phương thức <code><strong>includes()</strong></code> kiểm tra xem phần tử đã cho có tồn tại trong mảng hay không, trả về kết quả <code>true</code> hoặc <code>false</code>.</p>
<div>{{EmbedInteractiveExample("pages/js/array-includes.html")}}</div>
<h2 id="Cú_pháp">Cú pháp</h2>
<pre class="syntaxbox"><var>arr</var>.includes(<var>valueToFind</var>[, <var>fromIndex</var>])
</pre>
<h3 id="Các_tham_số">Các tham số</h3>
<dl>
<dt><code><var>valueToFind</var></code></dt>
<dd>
<p>Giá trị muốn kiểm tra.</p>
<div class="blockIndicator note">
<p><strong>Lưu ý:</strong> Khi kiểm tra chuỗi hoặc kí tự, <code>includes()</code> sẽ <strong>phân biệt hoa thường</strong>.</p>
</div>
</dd>
<dt><code><var>fromIndex</var></code> {{optional_inline}}</dt>
<dd>Vị trí trong mảng để bắt đầu tìm kiếm <code><var>valueToFind</var></code>; đầu tìm kiếm tại <code><var>fromIndex</var></code> khi <code><var>fromIndex</var></code> mang giá trị dương, hoặc tại <code>array.length + fromIndex</code> khi <code><var>fromIndex</var></code> mang giá trị âm (sử dụng {{interwiki("wikipedia", "absolute value", "giá trị tuyệt đối")}} của <code><var>fromIndex</var></code> 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.</dd>
</dl>
<h3 id="Giá_trị_trả_về">Giá trị trả về</h3>
<p>Có kiểu {{jsxref("Boolean")}}, trả về <code>true</code> nếu <code><var>valueToFind</var></code> được tìm thấy trong mảng (hoặc một phần của mảng được xác định bởi <code><var>fromIndex</var></code> nếu có). Các giá trị "không" được coi là bằng nhau (-0 sẽ bằng 0 và +0), nhưng <code>false</code> thì không bằng 0.</p>
<div class="note">
<p><strong>Lưu ý:</strong> Về mặt kĩ thuật, <code>includes()</code> sử dụng thuật toán <code><a href="/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Same-value-zero_equality">sameValueZero</a></code> để kiểm tra phần tử đã cho có tìm thấy hay không.</p>
</div>
<h2 id="Ví_dụ">Ví dụ</h2>
<pre class="brush: js">[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
</pre>
<h3 id="fromIndex_lớn_hơn_hoặc_bằng_độ_dài_mảng"><code><var>fromIndex</var></code> lớn hơn hoặc bằng độ dài mảng</h3>
<p>Nếu <code><var>fromIndex</var></code> lớn hơn hoặc bằng độ dài mảng, trả về kết quả <code>false</code></p>
<pre class="brush: js">var arr = ['a', 'b', 'c'];
arr.includes('c', 3); // false
arr.includes('c', 100); // false</pre>
<h3 id="Computed_index_nhỏ_hơn_0">Computed index nhỏ hơn 0</h3>
<p>Nếu <code><var>fromIndex</var></code> là số âm, computed index sẽ được dùng làm vị trí bắt đầu để tìm kiếm <code><var>valueToFind</var></code>. Nếu computed index nhỏ hơn hoặc bằng <code>-1 * array.length</code>, phần tử đã cho sẽ được tìm kiếm trong toàn bộ mảng (tương tự như <code><var>fromIndex</var></code> bằng 0).</p>
<pre class="brush: js">// độ 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</pre>
<h3 id="includes_used_as_a_generic_method"><code>includes()</code> used as a generic method</h3>
<p><code>includes()</code> method is intentionally generic. It does not require <code>this</code> 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 <code>includes()</code> method called on the function's <a href="/en-US/docs/Web/JavaScript/Reference/Functions/arguments">arguments</a> object.</p>
<pre class="brush: js">(function() {
console.log([].includes.call(arguments, 'a')); // true
console.log([].includes.call(arguments, 'd')); // false
})('a','b','c');</pre>
<div class="hidden">
<p>Please do not add polyfills on reference articles. For more details and discussion, see <a href="https://discourse.mozilla.org/t/mdn-rfc-001-mdn-wiki-pages-shouldnt-be-a-distributor-of-polyfills/24500">https://discourse.mozilla.org/t/mdn-rfc-001-mdn-wiki-pages-shouldnt-be-a-distributor-of-polyfills/24500</a></p>
</div>
<h2 id="Đặc_tả">Đặc tả</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-array.prototype.includes', 'Array.prototype.includes')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES7', '#sec-array.prototype.includes', 'Array.prototype.includes')}}</td>
<td>{{Spec2('ES7')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="Khả_năng_tương_thích_của_trình_duyệt">Khả năng tương thích của trình duyệt</h2>
<div>
<p>{{Compat("javascript.builtins.Array.includes")}}</p>
</div>
<h2 id="Xem_thêm">Xem thêm</h2>
<ul>
<li>{{jsxref("TypedArray.prototype.includes()")}}</li>
<li>{{jsxref("String.prototype.includes()")}}</li>
<li>{{jsxref("Array.prototype.indexOf()")}}</li>
<li>{{jsxref("Array.prototype.find()")}}</li>
<li>{{jsxref("Array.prototype.findIndex()")}}</li>
</ul>
|