From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../reference/global_objects/array/some/index.html | 134 +++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 files/pt-br/web/javascript/reference/global_objects/array/some/index.html (limited to 'files/pt-br/web/javascript/reference/global_objects/array/some/index.html') diff --git a/files/pt-br/web/javascript/reference/global_objects/array/some/index.html b/files/pt-br/web/javascript/reference/global_objects/array/some/index.html new file mode 100644 index 0000000000..f4724488dd --- /dev/null +++ b/files/pt-br/web/javascript/reference/global_objects/array/some/index.html @@ -0,0 +1,134 @@ +--- +title: Array.prototype.some() +slug: Web/JavaScript/Reference/Global_Objects/Array/some +translation_of: Web/JavaScript/Reference/Global_Objects/Array/some +--- +
{{JSRef}}
+ +

O método some() testa se ao menos um dos elementos no array passa no teste implementado pela função atribuída e retorna um valor true ou false.

+ +

Sintaxe

+ +
arr.some(callback[, thisArg])
+ +

Parâmetros

+ +
+
callback
+
Função para testar cada elemento, recebendo três argumentos: +
+
currentValue
+
O valor atual do elemento sendo processado no array.
+
index
+
O índice do elemento atual sendo processado no array.
+
array
+
O array onde o método some() foi chamado.
+
+
+
thisArg
+
Opcional. Valor para usar como  this durante a execução do callback.
+
+ +

Valor de retorno

+ +

Esta função retorna true se a função callback retornar true para qualquer elemento do array; caso contrário, false.

+ +

Descrição

+ +

some() executa a função callback uma vez para cada elemento presente no array até achar um onde o callback retorne um valor true. Se em qualquer dos elementos o valor for encontrado, some() imediatamente retorna true. Caso contrario, some() retorna false. callback é invocado somente para índices do array que contenham valor definido; não é invocado para índices que foram deletados ou os quais nunca tiveram valor definido.

+ +

callback é invocado com três argumentos: o valor do elemento, o índice do elemento, e o array onde a função foi chamada.

+ +

Se o parâmetro thisArg foi passado ao some(), ele sera passado ao callback quando o mesmo for invocado, para ser usado como o valor de this internamente na função callback. Caso contrario, o valor {{jsxref("undefined")}} será passado para uso como this. O valor this observado pela callback  é determinado de acordo com as regras usuais para determinar o que é visto por uma função.

+ +

some() não altera o array dentro do qual ele é chamado. 

+ +

O intervalo de elementos processado por some() é definido antes da primeira invocação da callback. Elementos contidos no array antes da chamada some() ser iniciada não serão testados pela callback. Se algum elemento pertencente ao array for alterado pela callback, o valor passado para a callback será o valor do momento em que a função some() encontra o índice daquele elemento. Elementos deletados não são testados.

+ +

Exemplos

+ +

Testando valores de elementos de um array

+ +

O exemplo a seguir testa se algum elemento de um array é maior que 10.

+ +
function isBiggerThan10(element, index, array) {
+  return element > 10;
+}
+[2, 5, 8, 1, 4].some(isBiggerThan10);  // false
+[12, 5, 8, 1, 4].some(isBiggerThan10); // true
+
+ +

Testando valores de elementos de um array usando arrow functions

+ +

Arrow functions fornece uma sintaxe mais curta para o mesmo teste.

+ +
[2, 5, 8, 1, 4].some(elem => elem > 10);  // false
+[12, 5, 8, 1, 4].some(elem => elem > 10); // true
+
+ +

Polyfill

+ +

some() was added to the ECMA-262 standard in the 5th edition; as such it may not be present in all implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of some() in implementations which do not natively support it. This algorithm is exactly the one specified in ECMA-262, 5th edition, assuming {{jsxref("Object")}} and {{jsxref("TypeError")}} have their original values and that fun.call evaluates to the original value of {{jsxref("Function.prototype.call()")}}.

+ +
// Production steps of ECMA-262, Edition 5, 15.4.4.17
+// Reference: http://es5.github.io/#x15.4.4.17
+if (!Array.prototype.some) {
+  Array.prototype.some = function(fun/*, thisArg*/) {
+    'use strict';
+
+    if (this == null) {
+      throw new TypeError('Array.prototype.some called on null or undefined');
+    }
+
+    if (typeof fun !== 'function') {
+      throw new TypeError();
+    }
+
+    var t = Object(this);
+    var len = t.length >>> 0;
+
+    var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
+    for (var i = 0; i < len; i++) {
+      if (i in t && fun.call(thisArg, t[i], i, t)) {
+        return true;
+      }
+    }
+
+    return false;
+  };
+}
+
+ +

Especificações

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES5.1', '#sec-15.4.4.17', 'Array.prototype.some')}}{{Spec2('ES5.1')}}Definição inicial. Implementada em JavaScript 1.6.
{{SpecName('ES6', '#sec-array.prototype.some', 'Array.prototype.some')}}{{Spec2('ES6')}}
+ +

Compatibilidade em navegadores

+ +
{{Compat("javascript.builtins.Array.some")}}
+ +

Veja também

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