How to use findIndex() method In Javascript
findIndex() method is used to return the first index of the element in a given array that satisfies the provided testing function (passed in by the user while calling). Otherwise, if no data is found then the value of -1 is returned.
Example: Using the findIndex() method to access values.
Javascript
let jsonArrayValues = [ { "Name" : "w3wiki" , "Location" : "Noida" }, { "Courses" : [ "DSA self-paced course" , "DevOps Bootcamp" , "GATE preparation course" ], "Topics" : [ "Web development" , "Artificial Intelligence" , "Machine Learning" , "Data Science" , "Algorithms" ] } ]; let index = jsonArrayValues .findIndex(item => item.Name === "w3wiki" ); // Check if the item is found before trying to log it if (index !== -1) { console.log(jsonArrayValues[index]); } else { console.log( "Item not found" ); } |
{ Name: 'w3wiki', Location: 'Noida' }
How to Get a Value from a JSON Array in JavaScript ?
We are going to iterate or access the value of a JSON array in JavaScript. There are several ways and techniques to access values from the JSON Array which are explained in detail below and are further divided into several possible techniques.
Table of Content
- Using the array[index] method
- Using the array[key] method
- Using the array.slice method
- Using for…of the loop
- Using forEach()
- Using the map() method
- Using filter() method
- Using the find() method
- Using findIndex() method
- Using some() method
- Using recursive traversal of JSON array
- Using the custom reduce method
- Using custom classes