diff options
Diffstat (limited to 'files/pt-pt/web/javascript/reference/global_objects/array/includes/index.html')
| -rw-r--r-- | files/pt-pt/web/javascript/reference/global_objects/array/includes/index.html | 175 |
1 files changed, 0 insertions, 175 deletions
diff --git a/files/pt-pt/web/javascript/reference/global_objects/array/includes/index.html b/files/pt-pt/web/javascript/reference/global_objects/array/includes/index.html deleted file mode 100644 index 201d4c9526..0000000000 --- a/files/pt-pt/web/javascript/reference/global_objects/array/includes/index.html +++ /dev/null @@ -1,175 +0,0 @@ ---- -title: Array.prototype.includes() -slug: Web/JavaScript/Reference/Global_Objects/Array/includes -tags: - - Array - - JavaScript - - Method - - Prototype - - Reference - - polyfill -translation_of: Web/JavaScript/Reference/Global_Objects/Array/includes ---- -<div>{{JSRef}}</div> - -<p>O método <code><strong>includes()</strong></code> determina se um array contém um determinado elemento, devolvendo <code>true</code> ou <code>false</code>. É utilizado o algoritmo sameValueZero para determinar se o elemento especificado foi encontrado.</p> - -<div>{{EmbedInteractiveExample("pages/js/array-includes.html")}}</div> - - - -<h2 id="Syntax">Syntax</h2> - -<pre class="syntaxbox"><var>arr</var>.includes(<var>searchElement[</var>, <var>fromIndex]</var>) -</pre> - -<h3 id="Parameters">Parameters</h3> - -<dl> - <dt><code>searchElement</code></dt> - <dd>The element to search for.</dd> - <dt><code>fromIndex</code> {{optional_inline}}</dt> - <dd>The position in this array at which to begin searching for <code>searchElement</code>. A negative value searches from the index of <code>array.length - fromIndex</code> by asc. Defaults to 0.</dd> -</dl> - -<h3 id="Return_value">Return value</h3> - -<p>A {{jsxref("Boolean")}}.</p> - -<h2 id="Examples">Examples</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_is_greater_than_or_equal_to_the_array_length"><code>fromIndex</code> is greater than or equal to the array length</h3> - -<p>If <code>fromIndex</code> is greater than or equal to the length of the array, <code>false</code> is returned. The array will not be searched.</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_is_less_than_0">Computed index is less than 0</h3> - -<p>If <code>fromIndex</code> is negative, the computed index is calculated to be used as a position in the array at which to begin searching for <code>searchElement</code>. If the computed index is less than 0, the entire array will be searched.</p> - -<pre class="brush: js">// array length is 3 -// fromIndex is -100 -// computed index is 3 + (-100) = -97 - -var arr = ['a', 'b', 'c']; - -arr.includes('a', -100); // true -arr.includes('b', -100); // true -arr.includes('c', -100); // true</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> - -<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) { - - if (this == null) { - throw new TypeError('"this" is null or not defined'); - } - - // 1. Let O be ? ToObject(this value). - 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); - - function sameValueZero(x, y) { - return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y)); - } - - // 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. - if (sameValueZero(o[k], searchElement)) { - return true; - } - // c. Increase k by 1. - k++; - } - - // 8. Return false - return false; - } - }); -} -</pre> - -<p>If you need to support truly obsolete JavaScript engines that don't support <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty">Object.defineProperty</a></code>, it's best not to polyfill <code>Array.prototype</code> methods at all, as you can't make them non-enumerable.</p> - -<h2 id="Specifications">Specifications</h2> - -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Specification</th> - <th scope="col">Status</th> - <th scope="col">Comment</th> - </tr> - <tr> - <td>{{SpecName('ES7', '#sec-array.prototype.includes', 'Array.prototype.includes')}}</td> - <td>{{Spec2('ES7')}}</td> - <td>Initial definition.</td> - </tr> - <tr> - <td>{{SpecName('ESDraft', '#sec-array.prototype.includes', 'Array.prototype.includes')}}</td> - <td>{{Spec2('ESDraft')}}</td> - <td> </td> - </tr> - </tbody> -</table> - -<h2 id="Browser_compatibility">Browser compatibility</h2> - -<div> - - -<p>{{Compat("javascript.builtins.Array.includes")}}</p> -</div> - -<h2 id="See_also">See also</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> |
