
En ese caso, los elementos procesados por find son establecidos antes de la primera invocación de callback. Si no se proporciona el parámetro, entonces se utiliza undefined.Įl método find no transforma el array desde el cual es llamado, pero la función proporcionada en callback sí. Si un parámetro thisArg es proporcionado al método find, este será utilizado como this para cada invocación del callback. En caso contrario, find devuelve undefined.Ĭallback se invoca con tres argumentos: el valor del elemento, el índice del elemento y el objeto Array que está siendo recorrido. Si es así, find devuelve inmediatamente el valor del elemento.
()Įl método find ejecuta la función callback una vez por cada índice del array hasta que encuentre uno en el que el callback devuelva un valor verdadero. Object.prototype._lookupSetter_() (en-US) Obsoleto. Object.prototype._lookupGetter_() (en-US) Obsoleto. Object.prototype._defineSetter_() (en-US) Obsoleto. Object.prototype._defineGetter_() Obsoleto. The array.findIndex() method takes a predicate and returns the index of the first element in the array that satisfies it. Example 2: Passing predicate to the findIndex() method In contrast, on the other side, the findIndex() method expects a function that has defined the logic on which we get the index. The main difference between findIndex() and indexOf()is that we need to provide the element to the indexOf() function. The findIndex() method will return the index based on the written logic inside the given function. So the first item, higher than 10, is 18, and its index is 2. Then, run that file by typing the node app on the terminal. If the item value > 10 is found, it will log that item’s index. Pass the arrow function, which checks if the item in the Array is higher than 10. In the above example, we have defined an array and the call the findIndex() function on that Array. Let ages = console.log(ages.findIndex(age => age > 10))