Muho
Muho

Reputation: 3536

search in object tree depending on depth value

I have an object that has random keys and values of other objects where the last depth of the tree has an id.

I want a create a function that takes a string and depth to get the child object depending on those two values

for example, I have the data like that:

var treeObj = {
    "random key1": {
        "random key11": {
            "random key11": {
                "random key11": {
                    id: 25,
                    count: 364
                },
                "random key12": {
                    id: 52,
                    count: 644
                },
                "random key13": {
                    id: 5,
                    count: 664
                },
                "random key14": {
                    id: 5,
                    count: 624
                }
            }
        },
        "random key2": {
            "random key21": {
                "random key21": {
                    id: 56,
                    count: 64
                }
            },
            "random key22": {
                "random key221": {
                    id: 56,
                    count: 64
                },
                "random key222": {
                    id: 35,
                    count: 64
                }
            },
        }
    },
    "other random key":{
        "different random key":{
            "different random key": {
                "different random key1": {
                    id: 75,
                    count: 664
                },
                "different random key2": {
                    id: 57,
                    count: 624
                },
                "different random key3": {
                    id: 50,
                    count: 654
                },
            },
        },
        "different random key2":{
            "different random key2": {
                "different random key21": {
                    id: 15,
                    count: 64
                },
            },
        }
    }
}

what I want is a function like:

get_child_obj('random key1', 0)

should return this:

{
    "random key11": {
        "random key11": {
            "random key11": {
                id: 25,
                count: 364
            },
            "random key12": {
                id: 52,
                count: 644
            },
            "random key13": {
                id: 5,
                count: 664
            },
            "random key14": {
                id: 5,
                count: 624
            }
        }
    },
    "random key2": {
        "random key21": {
            "random key21": {
                id: 56,
                count: 64
            }
        },
        "random key22": {
            "random key221": {
                id: 56,
                count: 64
            },
            "random key222": {
                id: 35,
                count: 64
            }
        },
    }
}

and a function like that:

get_child_obj('different random key', 2)

should return this:

{
    "different random key1": {
        id: 75,
        count: 664
    },
    "different random key2": {
        id: 57,
        count: 624
    },
    "different random key3": {
        id: 50,
        count: 654
    },
}

and a function like that:

get_child_obj('random key12', 3)

should return this:

{
    id: 52,
    count: 644
}

please note that for each depth the key is unique.

Upvotes: 1

Views: 148

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386600

You could take a recursive approach by handing over the object for seraching and by decrementing the depth for each nested level.

If depth is zero return the wanted property, otherwise iterate the values of the object and check if the nested object contains the wanted property.

function getChild(object, key, depth) {
    var result;
    return depth
        ? (Object.values(object).some(v => result = getChild(v, key, depth - 1)), result)
        : object[key];
}

var tree = { "random key1": { "random key11": { "random key11": { "random key11": { id: 25, count: 364 }, "random key12": { id: 52, count: 644 }, "random key13": { id: 5, count: 664 }, "random key14": { id: 5, count: 624 } } }, "random key2": { "random key21": { "random key21": { id: 56, count: 64 } }, "random key22": { "random key221": { id: 56, count: 64 }, "random key222": { id: 35, count: 64 } } } }, "other random key": { "different random key": { "different random key": { "different random key1": { id: 75, count: 664 }, "different random key2": { id: 57, count: 624 }, "different random key3": { id: 50, count: 654 } } }, "different random key2": { "different random key2": { "different random key21": { id: 15, count: 64 } } } } };

console.log(getChild(tree, 'random key1', 0));
console.log(getChild(tree, 'different random key', 2));
console.log(getChild(tree, 'random key12', 3));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Related Questions