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
-
Economics & Finance
Selected Reading
How to access nested JSON property based on another property's value in JavaScript?
To access a nested JSON property based on another property's value in JavaScript, you can loop through the data and match the target property. This technique is useful when searching for specific records in JSON arrays.
Example: Finding Student Marks by Subject
var actualJSONData = JSON.parse(studentDetails());
var 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;
}
}
return null; // Return null if subject not found
}
function studentDetails() {
return JSON.stringify([
{ firstName: "John", subjectName: "JavaScript", marks: 97 },
{ firstName: "David", subjectName: "Java", marks: 98 }
]);
}
The student marks = 97
Using find() Method
A more modern approach uses the find() method for cleaner code:
var jsonData = [
{ firstName: "John", subjectName: "JavaScript", marks: 97 },
{ firstName: "David", subjectName: "Java", marks: 98 },
{ firstName: "Mike", subjectName: "Python", marks: 95 }
];
function getMarksBySubject(data, subject) {
var student = data.find(item => item.subjectName === subject);
return student ? student.marks : null;
}
console.log("JavaScript marks:", getMarksBySubject(jsonData, "JavaScript"));
console.log("Python marks:", getMarksBySubject(jsonData, "Python"));
console.log("C++ marks:", getMarksBySubject(jsonData, "C++"));
JavaScript marks: 97 Python marks: 95 C++ marks: null
Accessing Deeply Nested Properties
For more complex nested structures, you can access deeper properties:
var complexData = [
{
student: { name: "John", id: 1 },
subjects: [
{ name: "JavaScript", grades: { midterm: 85, final: 92 } },
{ name: "Java", grades: { midterm: 88, final: 90 } }
]
},
{
student: { name: "Alice", id: 2 },
subjects: [
{ name: "Python", grades: { midterm: 90, final: 95 } }
]
}
];
function getFinalGrade(data, studentName, subjectName) {
var studentRecord = data.find(record => record.student.name === studentName);
if (!studentRecord) return null;
var subject = studentRecord.subjects.find(sub => sub.name === subjectName);
return subject ? subject.grades.final : null;
}
console.log("John's JavaScript final:", getFinalGrade(complexData, "John", "JavaScript"));
console.log("Alice's Python final:", getFinalGrade(complexData, "Alice", "Python"));
John's JavaScript final: 92 Alice's Python final: 95
Key Points
- Use
===for strict equality comparison, not=(assignment) - The
find()method returns the first matching element orundefined - Always handle cases where the property might not exist
- For complex nested data, chain your property access carefully
Conclusion
Accessing nested JSON properties based on other property values is straightforward using loops or the find() method. Always use strict equality (===) and handle cases where properties don't exist.
Advertisements
