Home  >  Article  >  Web Front-end  >  How to Find a Specific Object within a Nested JavaScript Object by Label?

How to Find a Specific Object within a Nested JavaScript Object by Label?

Linda Hamilton
Linda HamiltonOriginal
2024-11-03 21:13:29355browse

How to Find a Specific Object within  a Nested JavaScript Object by Label?

Iterating Nested JavaScript Objects: Identifying Objects by Label

To traverse a nested JavaScript object and locate an object with a specific label, follow these steps:

Implementation Using Recursion

To recursively iterate through the nested object:

const iterate = (obj) => {
    Object.keys(obj).forEach(key => {
        console.log(`key: ${key}, value: ${obj[key]}`)

        if (typeof obj[key] === 'object' && obj[key] !== null) {
            iterate(obj[key])
        }
    })
}
iterate(obj); // obj is the main object to be traversed

Implementation Using Non-Recursive Approach (2023 Update)

For a non-recursive approach:

const iterate = (obj) => {
    const stack = [obj];
    while (stack?.length > 0) {
        const currentObj = stack.pop();
        Object.keys(currentObj).forEach(key => {
            console.log(`key: ${key}, value: ${currentObj[key]}`);
            if (typeof currentObj[key] === 'object' && currentObj[key] !== null) {
                stack.push(currentObj[key]);
            }
        });
    }
};

Example Object

Consider the following nested object:

var cars = {
    label: 'Autos',
    subs: [
        {
            label: 'SUVs',
            subs: []
        },
        {
            label: 'Trucks',
            subs: [
                {
                    label: '2 Wheel Drive',
                    subs: []
                },
                {
                    label: '4 Wheel Drive',
                    subs: [
                        {
                            label: 'Ford',
                            subs: []
                        },
                        {
                            label: 'Chevrolet',
                            subs: []
                        }
                    ]
                }
            ]
        },
        {
            label: 'Sedan',
            subs: []
        }
    ]
};

Using the Identifier

To retrieve the object with the label "4 Wheel Drive" using the recursive approach, call:

iterate(cars);

To retrieve the same object using the non-recursive approach, call:

iterate(cars);

This will log all the key-value pairs in the nested object, including the desired object.

The above is the detailed content of How to Find a Specific Object within a Nested JavaScript Object by Label?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn