Esta tradução está incompleta. Ajude atraduzir este artigo.
O método some()
testa se alguns dos elementos no array passam no teste implementado pela função atribuída.
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 docallback
.
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 undefined
será passado para uso como this. O valor this
ultimately observable by callback
is determined according to the usual rules for determining the this
seen by a function.
some()
não altera o array dentro do qual ele é chamado.
The range of elements processed by some()
is set before the first invocation of callback
. Elements that are appended to the array after the call to some()
begins will not be visited by callback
. If an existing, unvisited element of the array is changed by callback
, its value passed to the visiting callback
will be the value at the time that some()
visits that element's index; elements that are deleted are not visited.
Exemplos
Testando valores de elementos de um array
The following example tests whether any element in the array is bigger than 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
Testing array elements using arrow functions
Arrow functions provide a shorter syntax for the same test.
[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 Object
and TypeError
have their original values and that fun.call
evaluates to the original value of Function.prototype.call()
.
// Production steps of ECMA-262, Edition 5, 15.4.4.17 // Reference: https://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; }; }
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 5.1 (ECMA-262) The definition of 'Array.prototype.some' in that specification. |
Standard | Initial definition. Implemented in JavaScript 1.6. |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.prototype.some' in that specification. |
Standard |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | 1.5 (1.8) | 9 | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | 1.0 (1.8) | (Yes) | (Yes) | (Yes) |