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
|
---
title: Array.prototype.includes()
slug: Web/JavaScript/Reference/Global_Objects/Array/contains
tags:
- Array
- ECMAScript7
- Experimental
- Expérimental(2)
- JavaScript
translation_of: Web/JavaScript/Reference/Global_Objects/Array/includes
---
<div>{{JSRef("Global_Objects", "Array")}}</div>
<h2 id="Sumário">Sumário</h2>
<p><span class="seoSummary">O método includes<code>()</code> determina se um array contém um determinado elemento, retornando <code>true</code> ou <code>false</code> apropriadamente.</span></p>
<p><strong style="font-size: 2.14285714285714rem; font-weight: 700; letter-spacing: -1px; line-height: 30px;">Sintaxe</strong></p>
<pre class="syntaxbox"><code><var>array</var>.includes(<var>searchElement</var>[, <var>fromIndex</var>])</code></pre>
<h3 id="Parâmetros">Parâmetros</h3>
<dl>
<dt><code>searchElement</code></dt>
<dd>O elemento a buscar</dd>
<dt><code>fromIndex</code></dt>
<dd>Opcional. A posição no array de onde a busca pelo <code>searchElement </code>se iniciará. Por padrão, 0.</dd>
</dl>
<h2 id="Exemplos">Exemplos</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>
<h2 id="Polyfill">Polyfill</h2>
<pre class="brush: js">// https://tc39.github.io/ecma262/#sec-array.prototype.includes
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', {
value: function(searchElement, fromIndex) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// 3. If len is 0, return false.
if (len === 0) {
return false;
}
// 4. Let n be ? ToInteger(fromIndex).
// (If fromIndex is undefined, this step produces the value 0.)
var n = fromIndex | 0;
// 5. If n ≥ 0, then
// a. Let k be n.
// 6. Else n < 0,
// a. Let k be len + n.
// b. If k < 0, let k be 0.
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
// 7. Repeat, while k < len
while (k < len) {
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
// b. If SameValueZero(searchElement, elementK) is true, return true.
// c. Increase k by 1.
// NOTE: === provides the correct "SameValueZero" comparison needed here.
if (o[k] === searchElement) {
return true;
}
k++;
}
// 8. Return false
return false;
}
});
}
</pre>
<h2 id="Especificações">Especificações</h2>
<p>Proposta ES7: <a href="https://github.com/domenic/Array.prototype.contains/blob/master/spec.md">https://github.com/domenic/Array.prototype.contains/blob/master/spec.md</a></p>
<h2 id="Compatibilidade">Compatibilidade</h2>
<div>{{Compat("javascript.builtins.Array.includes")}}</div>
<h2 id="Veja_Também">Veja Também</h2>
<ul>
<li>{{jsxref("TypedArray.prototype.includes()")}}</li>
<li>{{jsxref("String.prototype.includes()")}}</li>
<li>{{jsxref("Array.prototype.indexOf()")}}</li>
</ul>
|