根据深度值在对象树中搜索

时间:2019-03-04 15:33:24

标签: javascript json tree

我有一个对象,该对象具有随机键和其他对象的值,其中树的最后深度具有ID。

我想要创建一个函数,该函数需要一个字符串和深度来获取取决于这两个值的子对象

例如,我有这样的数据:

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
                },
            },
        }
    }
}

我想要的是类似这样的功能

get_child_obj('random key1',0)

应返回以下内容:

{
    "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
            }
        },
    }
}

和类似的功能:

get_child_obj('different random key', 2)

应返回以下内容:

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

和类似的功能:

get_child_obj('random key12', 3)

应返回以下内容:

{
    id: 52,
    count: 644
}

请注意,每个深度的键都是唯一的。

1 个答案:

答案 0 :(得分:1)

您可以采用递归方法,即移交对象进行搜索,并减小每个嵌套层的深度。

如果depth为零,则返回所需属性,否则迭代该对象的值并检查嵌套对象是否包含该所需属性。

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; }