Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Find Object in Array by Property Value</title> </head> <body> <script> // Sample array var myArray = [ {"id": 1, "name": "Alice"}, {"id": 2, "name": "Peter"}, {"id": 3, "name": "Harry"} ]; // Get the index of Array item which matchs the id "2" var index = myArray.findIndex(item => item.id === 2); document.write(index + "<br>"); // Prints: 1 document.write(myArray[index].name); // Prints: Peter </script> </body> </html>