How to usethe Array.find() method in JSON

In this approach, we are going to use the Array.find() method which is used to find the value in an array.

Example: In this example, we will use Array.find() to find the Index of the object.

Javascript




let data = [{
    "name": "HTML",
    "description": "HyperText Markup Language"
}, {
    "name": "CSS",
    "description": "Cascade Style Sheet"
}, {
    "name": "JS",
    "description": "JavaScript"
}]
let index = -1;
let val = "JS"
let filteredObj = data.find(function (item, i) {
    if (item.name === val) {
        index = i;
        return i;
    }
});
 
if (index == -1) {
    console.log("Data not found!")
} else {
    console.log(filteredObj.name, "is at index", index);
}


Output:

JS is at index 2

How can I get the index from a JSON object with value ?

In this article, we are going to learn how you get an index from a JSON object with value. we have given a value we need to search for that value with its key in the given JSON object and print the result in the console.

These are the following approaches:

Table of Content

  • Using the Array.find() method
  • Using findIndex() method

Similar Reads

Approach 1: Using the Array.find() method

In this approach, we are going to use the Array.find() method which is used to find the value in an array....

Approach 2: Using findIndex() method

...

Contact Us