Extract key value from a nested object in JavaScript?

Extracting keys from nested objects is a common requirement in JavaScript. This tutorial shows how to find the parent object and nested property that contains a specific value.

Creating a Nested Object

Let's start with a nested object containing teacher and subject information:

var details = {
    "teacherDetails": {
        "teacherName": ["John", "David"]
    },
    "subjectDetails": {
        "subjectName": ["MongoDB", "Java"]
    }
}

console.log("Nested object created:", details);
Nested object created: {
  teacherDetails: { teacherName: [ 'John', 'David' ] },
  subjectDetails: { subjectName: [ 'MongoDB', 'Java' ] }
}

Extracting Keys by Value

To find which parent object and nested key contains a specific value, we can use nested loops:

var details = {
    "teacherDetails": {
        "teacherName": ["John", "David"]
    },
    "subjectDetails": {
        "subjectName": ["MongoDB", "Java"]
    }
}

var objectName, nestedObject;
var searchValue = "Java";

for(var key in details){
    for(var secondKey in details[key]){
        if(details[key][secondKey].includes(searchValue)){
            objectName = key;
            nestedObject = secondKey;
        }
    }
}

console.log("Found '" + searchValue + "' in:");
console.log("Parent key: " + objectName);
console.log("Nested key: " + nestedObject);
console.log("Combined: " + objectName + ", " + nestedObject);
Found 'Java' in:
Parent key: subjectDetails
Nested key: subjectName
Combined: subjectDetails, subjectName

Enhanced Version with Function

Here's a reusable function approach for better code organization:

function findKeysForValue(obj, searchValue) {
    for(let parentKey in obj) {
        for(let nestedKey in obj[parentKey]) {
            if(Array.isArray(obj[parentKey][nestedKey]) && 
               obj[parentKey][nestedKey].includes(searchValue)) {
                return {
                    parentKey: parentKey,
                    nestedKey: nestedKey
                };
            }
        }
    }
    return null;
}

var details = {
    "teacherDetails": {
        "teacherName": ["John", "David"]
    },
    "subjectDetails": {
        "subjectName": ["MongoDB", "Java"]
    }
}

var result = findKeysForValue(details, "David");
if(result) {
    console.log("Parent key:", result.parentKey);
    console.log("Nested key:", result.nestedKey);
} else {
    console.log("Value not found");
}
Parent key: teacherDetails
Nested key: teacherName

How It Works

The algorithm uses two nested loops:

  • Outer loop: Iterates through parent object keys (teacherDetails, subjectDetails)
  • Inner loop: Iterates through nested object keys (teacherName, subjectName)
  • Check: Uses includes() method to search for the target value in arrays

Conclusion

Nested loops provide an effective way to extract keys from nested objects based on values. The function approach offers better reusability for complex applications.

Updated on: 2026-03-15T23:18:59+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements