Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
How to access nested JSON property based on another property's value in JavaScript?
To access nested JSON property based on another property’s value, the code is as follows −
Example
var actualJSONData = JSON.parse(studentDetails()),
studentMarks = getMarksUsingSubjectName(actualJSONData, "JavaScript");
console.log("The student marks="+studentMarks);
function getMarksUsingSubjectName(actualJSONData, givenSubjectName){
for(var tempObj of actualJSONData){
if(tempObj.subjectName = givenSubjectName){
return tempObj.marks;
}
}
}
function studentDetails(){
return JSON.stringify(
[
{ firstName : "John", subjectName: "JavaScript", marks : 97 },
{ firstName : "David", subjectName: "Java", marks : 98 }
]
);
}
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo155.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo155.js The student marks=97
Advertisements
