Array.findIndex





'Il findIndex()metodo restituisce l'indice del primo elemento dell'array che passa una funzione di test.

'Questo esempio trova l'indice del primo elemento che e' maggiore di 18:

'Esempio

'var numbers = [4, 9, 16, 25, 29];

'var first = numbers.findIndex(myFunction);

'function myFunction(value, index, array) {

' return value > 18;

'}


https://www.w3schools.com/js/tryit.asp?filename=tryjs_array_find_index

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array.findIndex()</h2>

<p id="demo"></p>

<script>
var numbers = [4, 9, 16, 25, 29];
var first = numbers.findIndex(myFunction);

document.getElementById("demo").innerHTML = "First number over 18 has index " + first;

function myFunction(value, index, array) {
return value > 18;
}
</script>

</body>
</html>

'Si noti che la funzione accetta 3 argomenti:

'Il valore dell'oggetto

'L'indice dell'oggetto

'La matrice stessa










( arrayfindindex.html )- by Paolo Puglisi - Modifica del 17/12/2023