Check Whether a Value Exists in JSON Object



Let’s say the following is our object −

var apiJSONObject =
[
   {subjectName:"MySQL"},
   {subjectName:"Java"},
   {subjectName:"JavaScript"},
   {subjectName:"MongoDB"}
]

Let’s check for existence of a value “JavaScript” −

Example

var apiJSONObject =
[
   {subjectName:"MySQL"},
   {subjectName:"Java"},
   {subjectName:"JavaScript"},
   {subjectName:"MongoDB"}
]
for(var i=0;i<apiJSONObject.length;i++){
   if(apiJSONObject[i].subjectName=="JavaScript"){
      console.log("The search found in JSON Object");
      break;
   }
}

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo117.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo117.js
The search found in JSON Object
Updated on: 2020-09-09T13:28:22+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements