- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- How to access nested json objects in JavaScript?
- Access property as a property using 'get' in JavaScript?
- Grouping objects based on key property in JavaScript
- How to sort a JavaScript object list based on a property when the property is not consistent
- How to access a function property as a method in JavaScript?
- Error: Permission denied to access property ‘target’ in JavaScript
- How to find the proportion of categories based on another categorical column in R's data.table object?
- map() array of object titles into a new array based on other property value JavaScript
- Removing property from a JSON object in JavaScript
- Get value for key from nested JSON object in JavaScript
- How to convert a date object's content into json in JavaScript?
- JavaScript - How to create nested unordered list based on nesting of array?
- How to get Euler's constant value in JavaScript?
- How do I change a tag's inner HTML based on their order in JavaScript?
- Print JSON nested object in JavaScript?

Advertisements