diff options
author | Florian Merz <me@fiji-flo.de> | 2021-02-11 14:49:58 +0100 |
---|---|---|
committer | Florian Merz <me@fiji-flo.de> | 2021-02-11 14:49:58 +0100 |
commit | 68fc8e96a9629e73469ed457abd955e548ec670c (patch) | |
tree | 8529ab9fe63d011f23c7f22ab5a4a1c5563fcaa4 /files/pt-br/web/javascript/reference/global_objects/array/includes | |
parent | 8260a606c143e6b55a467edf017a56bdcd6cba7e (diff) | |
download | translated-content-68fc8e96a9629e73469ed457abd955e548ec670c.tar.gz translated-content-68fc8e96a9629e73469ed457abd955e548ec670c.tar.bz2 translated-content-68fc8e96a9629e73469ed457abd955e548ec670c.zip |
unslug pt-br: move
Diffstat (limited to 'files/pt-br/web/javascript/reference/global_objects/array/includes')
-rw-r--r-- | files/pt-br/web/javascript/reference/global_objects/array/includes/index.html | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/files/pt-br/web/javascript/reference/global_objects/array/includes/index.html b/files/pt-br/web/javascript/reference/global_objects/array/includes/index.html new file mode 100644 index 0000000000..a0f794df1a --- /dev/null +++ b/files/pt-br/web/javascript/reference/global_objects/array/includes/index.html @@ -0,0 +1,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> |