Home > Web Front-end > JS Tutorial > 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
Release: 2024-11-03 21:13:29
Original
404 people have browsed it

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
Copy after login

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]);
            }
        });
    }
};
Copy after login

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: []
        }
    ]
};
Copy after login

Using the Identifier

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

iterate(cars);
Copy after login
Copy after login

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

iterate(cars);
Copy after login
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template