为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

当年话下 2021-06-04 18:48:11
抱歉,我不知道如何正确表达这个问题,但基本上,我有这个嵌套对象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 中,当我拥有某个节点的“路径”时,有没有办法动态处理嵌套对象。
查看完整描述

3 回答

?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

您可以使用 aObject.keys循环遍历dataPath对象。然后你split的价值,并使用它一个for 循环并跟踪data.


let 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"

        }

    }

}


let dataPath = {

    "property1": "subProperty1.data",

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

    "property3": "subProperty1.data",

}


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

for (let property of properties) {

    let path = dataPath[property].split('.')

    let data = nestedObj[property]

    for (let i = 0; i < path.length; i++) {

        data = data[path[i]]

        if (i + 1 === path.length) {

            console.log(data)

        }

    }

}


查看完整回答
反对 回复 2021-06-11
?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

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 => {

  const nestedObjAtProperty = nestedObj[property]; // { "subProperty1": { "data": "This is the data for property1 > subProperty1" } }

  const dataPathForProperty = dataPath[property]; // "subProperty1.data"


  const data = _.get(nestedObjAtProperty, dataPathForProperty); // "This is the data for property1 > subProperty1"

  console.log(data);

}


查看完整回答
反对 回复 2021-06-11
  • 3 回答
  • 0 关注
  • 118 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信