JSON find the First index of an object

JSON find the First index of an object

In the following example, we will find the first index of an object in an array with a name property is equal to 'Mike'.


HTML Editor
<!DOCTYPE html> <html lang="en-US"> <body> <h1>JSON findIndex</h1> <button onclick="myFunction()">Click Me</button> <script> var data = [ { "name": "Danny", "age": 25 }, { "name": "Mike", "age": 25 }, { "name": "Danny", "age": 18 } ] var data1 = [];function myFunction(){ for(var i = 0; i < data.length; i++){ if (data[i].name == "Mike"){ console.log(i); return 0; } } } </script> </body> </html>

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author