猿问

如果我不知道深度是多少,如何在 Javascript 中动态处理嵌套对象?

抱歉,我不知道如何正确表达这个问题,但基本上,我有这个嵌套对象nestedObj。我想动态处理这个对象,但对象的深度不固定。例如:


nestedObj = {  

    "property1":{

        "subProperty1": {  

            "data": "This is the data for property1 > subProperty1"

        }

    },


    "property2":{

        "subProperty1": {

            "anotherSubProperty1": {

                "data": "This is the data for property2 > subProperty1 > anotherSubProperty1"

            }

        },

        "subProperty2": {

            "anotherSubProperty1": {

                "data": "This is the data for property2 > subProperty2 > anotherSubProperty1"

            }

        }

    },


    "property3":{

        "subProperty1": {  

            "data": "This is the data for property3 > subProperty1"

        }

    }

}

现在我想在一个for of循环中处理这个对象,我的最终目标是获取data某些节点的值。现在,每个对象节点的值的路径在另一个对象中定义:


dataPath = {

    "property1": "subProperty1.data",

    "property2": "subProperty2.anotherSubProperty1.data",

    "property3": "subProperty1.data",

}

然后我用这个for of循环来处理它们:


properties = ["property1", "property2", "property3"];

for(var property of properties) {

    path_to_data = dataPath[property].split(".");

    data = nestedObj[path_to_data[0]][path_to_data[1]];


    // Some other logic

}

显然,这只适用于“property1”和“property3”。我必须为所有具有 2 个以上内部节点的对象添加特殊处理。对于具有不同深度的大数据,手动添加这些特殊处理程序并不理想。所以我的问题是,在 Javascript 中,当我拥有某个节点的“路径”时,有没有办法动态处理嵌套对象。


当年话下
浏览 119回答 3
3回答

一只萌萌小番薯

您可以使用 aObject.keys循环遍历dataPath对象。然后你split的价值,并使用它一个for 循环并跟踪data.let nestedObj = {&nbsp; &nbsp; "property1": {&nbsp; &nbsp; &nbsp; &nbsp; "subProperty1": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "data": "This is the data for property1 > subProperty1"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; "property2": {&nbsp; &nbsp; &nbsp; &nbsp; "subProperty1": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "anotherSubProperty1": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "data": "This is the data for property2 > subProperty1 > anotherSubProperty1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; "subProperty2": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "anotherSubProperty1": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "data": "This is the data for property2 > subProperty2 > anotherSubProperty1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; "property3": {&nbsp; &nbsp; &nbsp; &nbsp; "subProperty1": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "data": "This is the data for property3 > subProperty1"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}let dataPath = {&nbsp; &nbsp; "property1": "subProperty1.data",&nbsp; &nbsp; "property2": "subProperty2.anotherSubProperty1.data",&nbsp; &nbsp; "property3": "subProperty1.data",}let properties = ["property1", "property2", "property3"]for (let property of properties) {&nbsp; &nbsp; let path = dataPath[property].split('.')&nbsp; &nbsp; let data = nestedObj[property]&nbsp; &nbsp; for (let i = 0; i < path.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; data = data[path[i]]&nbsp; &nbsp; &nbsp; &nbsp; if (i + 1 === path.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(data)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

江户川乱折腾

lodash 的_.get功能正是您想要的。您可以查看源代码以查看实现。const nestedObj = { "property1":{ "subProperty1": { "data": "This is the data for property1 > subProperty1" } }, "property2":{ "subProperty1": { "anotherSubProperty1": { "data": "This is the data for property2 > subProperty1 > anotherSubProperty1" } }, "subProperty2": { "anotherSubProperty1": { "data": "This is the data for property2 > subProperty2 > anotherSubProperty1" } } }, "property3":{ "subProperty1": { "data": "This is the data for property3 > subProperty1" } } }const dataPath = { "property1": "subProperty1.data", "property2": "subProperty2.anotherSubProperty1.data", "property3": "subProperty1.data", }let props = ["property1", "property2", "property3"];props.forEach(property => {&nbsp; const nestedObjAtProperty = nestedObj[property]; // { "subProperty1": { "data": "This is the data for property1 > subProperty1" } }&nbsp; const dataPathForProperty = dataPath[property]; // "subProperty1.data"&nbsp; const data = _.get(nestedObjAtProperty, dataPathForProperty); // "This is the data for property1 > subProperty1"&nbsp; console.log(data);}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答