'Il find()metodo restituisce il valore del primo elemento dell'array che passa una funzione di test. 'Questo esempio trova (restituisce il valore di) il primo elemento che e' maggiore di 18: 'Esempio 'var numbers = [4, 9, 16, 25, 29]; 'var first = numbers.find(myFunction); 'function myFunction(value, index, array) { ' return value > 18; '} 'Si noti che la funzione accetta 3 argomenti: 'Il valore dell'oggetto 'L'indice dell'oggetto 'La matrice stessa https://www.w3schools.com/js/tryit.asp?filename=tryjs_array_find <!DOCTYPE html> <html> <body> <h2>JavaScript Array.find()</h2> <p id="demo"></p> <script> var numbers = [4, 9, 16, 25, 29]; var first = numbers.find(myFunction); document.getElementById("demo").innerHTML = "First number over 18 is " + first; function myFunction(value, index, array) { return value > 18; } </script> </body> </html> |