| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex | [Back] [Original] |
Get to know MDN better
Esta pgina foi traduzida do ingls pela comunidade. Saiba mais e junte-se comunidade MDN Web Docs.
This feature is well established and works across many devices and browser versions. Its been available across browsers since setembro de 2015.
O mtodo findIndex() retorna o ndice no array do primeiro elemento que satisfizer a funo de teste provida. Caso contrrio, retorna -1, indicando que nenhum elemento passou no teste.
Veja tambm o mtodo find(), que retorna o valor de um elemento encontrado no array em vez de seu ndice.
arr.findIndex(callback[, thisArg])
callbackFuno para executar em cada valor no array, tomando trs argumentos:
thisArgOpcional. Objeto para usar como this na execuo do callback.
O mtodo findIndex executa a funo callback uma vez para cada elemento presente no array at encontrar um onde o callback retorna um valor verdadeiro. Se tal elemento for encontrado, findIndex imediatamente retorna o ndice deste elemento. Caso contrrio, findIndex retorna -1. callback invocado apenas para ndices no array que tm valores atribudos; nunca invocado para ndices que foram deletados ou que nunca tiveram valores atribudos.
callback invocado com trs argumentos: o valor do elemento, o ndice do elemento e o objeto Array sendo percorrido.
Se um parmetro thisArg for fornecido para findIndex, ele ser usado como o this para cada invocao do callback. Se no for fornecido, ento undefined usado.
findIndex no modifica o array sobre o qual chamado.
A srie de elementos processados por findIndex definida antes da primeira invocao do callback. Elementos que so adicionados ao array depois que a chamada a findIndex comea no sero visitados pelo callback. Se um elemento existente no visitado do array for modificado pelo callback, seu valor passado ao callback ser o valor no momento em que findIndex visitar o ndice deste elemento; elementos que forem deletados no so visitados.
O seguinte exemplo encontra o ndice de um elemento no array que um nmero primo (ou retorna -1 se no houver nmero primo).
function isPrime(element, index, array) {
var start = 2;
while (start <= Math.sqrt(element)) {
if (element % start++ < 1) {
return false;
}
}
return element > 1;
}
console.log([4, 6, 8, 12].findIndex(isPrime)); // -1, no encontrado
console.log([4, 6, 7, 12].findIndex(isPrime)); // 2
Esse mtodo foi adicionado especificao do ECMAScript 6 e pode no estar disponvel em todas as implementaes de JavaScript ainda. Contudo, voc pode fazer o polyfill de Array.prototype.findIndex com o seguinte trecho de cdigo:
if (!Array.prototype.findIndex) {
Array.prototype.findIndex = function (predicate) {
if (this === null) {
throw new TypeError(
"Array.prototype.findIndex called on null or undefined",
);
}
if (typeof predicate !== "function") {
throw new TypeError("predicate must be a function");
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return i;
}
}
return -1;
};
}
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-array.prototype.findindex |
This page was last modified on 24 de jun. de 2025 by MDN contributors.
ArrayArray.prototype.at()Array.prototype.concat()Array.prototype.copyWithin()Array.prototype.entries()Array.prototype.every()Array.prototype.fill()Array.prototype.filter()Array.prototype.find()Array.prototype.findIndex()findLast()findLastIndex()Array.prototype.flat()Array.prototype.flatMap()Array.prototype.forEach()Array.prototype.includes()Array.prototype.indexOf()Array.prototype.join()Array.prototype.keys()Array.prototype.lastIndexOf()Array.prototype.map()Array.prototype.pop()Array.prototype.push()Array.prototype.reduce()Array.prototype.reduceRight()Array.prototype.reverse()Array.prototype.shift()Array.prototype.slice()Array.prototype.some()Array.prototype.sort()Array.prototype.splice()Array.prototype.toLocaleString()toReversed()toSorted()toSpliced()Array.prototype.toString()Array.prototype.unshift()Array.prototype.values()with()Array.prototype[@@iterator]()Object/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |