Sometimes, we want to search inside a JSON object with JavaScript.
In this article, we’ll look at how to search inside a JSON object with JavaScript.
How to search inside a JSON object with JavaScript?
To search inside a JSON object with JavaScript, we use the array find
method.
For instance, we write
const data = {
list: [
{ name: "My Name", id: 1, type: "car owner" },
{ name: "My Name 2", id: 2, type: "car owner 2" },
{ name: "My Name 3", id: 3, type: "car owner 3" },
{ name: "My Name 4", id: 4, type: "car owner 4" },
],
};
const result = data.list.find((record) => record.name === "My Name");
to get the data.list
array property.
Then we use the find
method to get the first result that has the name
property equals 'My Name'
.
Conclusion
To search inside a JSON object with JavaScript, we use the array find
method.