使用迭代访问嵌套的 javascript 对象子属性

我有一个对象,我想从中访问子级的属性


var obj={

      "Total Cost of Ownership": {

        "Operational Cost": {

          "Asset Cost": {

            "Maintenance": {

              "Scheduled": {

                "Predictive": [

                  "Parts",

                  "Labours",

                  "Consumables"

                ],

                "Periodic": [

                  "Parts",

                  "Labours",

                  "Consumables"

                ]

              },

              "Unscheduled": [

                "Parts",

                "Labours",

                "Consumables"

              ],

              "Other Maintenance": [

                "Parts",

                "Labours",

                "Consumables"

              ]

            },

            "Compliance": [

              "Emissions",

              "HOS"

            ]

          },

          "Under Utilization Cost": [

            "Asset Unassigned",

            "LTL",

            "Empty Miles",

            "Downtime",

            "Idling Time",

            "Crew Unassigned Time"

          ],

          "Route Cost": {

            "Fuel": [

              "Defined Route",

              "Excess Miles",

              "Unattributable Miles"

            ],

            "Charging": {

              

            },

            "Wait Time": {

              

            },

            "Toll": {

              

            }

          },

          "Crew Cost": [

            "Driving Violations",

            "Slary & Insurance",

            "Training"

          ],

          "Unsafe Operations Cost": [

            "Fatalities",

            "Injuries",

            "Unsalvageable Vehicles"

          ]

        }

      }

    }

    


我有字符串,我想根据这些字符串返回对象孩子的


var str1 = "Total Cost of Ownership";

var str2 = "Total Cost of Ownership*Operational Cost*Asset Cost"

我为它写了一个函数


现在我希望函数应该根据函数输入返回 obj 的孩子,但我无法访问它。请求最佳解决方案?


慕田峪7331174
浏览 139回答 1
1回答

慕的地6264312

用这个function getChildOf(x){  var keys = x.split("*")  let tempObj = obj;  for (const key of keys) {        tempObj = tempObj[key]    }    return tempObj;}// Test with your datavar obj={  "Total Cost of Ownership": {    "Operational Cost": {      "Asset Cost": {        "Maintenance": {          "Scheduled": {            "Predictive": [              "Parts",              "Labours",              "Consumables"            ],            "Periodic": [              "Parts",              "Labours",              "Consumables"            ]          },          "Unscheduled": [            "Parts",            "Labours",            "Consumables"          ],          "Other Maintenance": [            "Parts",            "Labours",            "Consumables"          ]        },        "Compliance": [          "Emissions",          "HOS"        ]      },      "Under Utilization Cost": [        "Asset Unassigned",        "LTL",        "Empty Miles",        "Downtime",        "Idling Time",        "Crew Unassigned Time"      ],      "Route Cost": {        "Fuel": [          "Defined Route",          "Excess Miles",          "Unattributable Miles"        ],        "Charging": {        },        "Wait Time": {        },        "Toll": {        }      },      "Crew Cost": [        "Driving Violations",        "Slary & Insurance",        "Training"      ],      "Unsafe Operations Cost": [        "Fatalities",        "Injuries",        "Unsalvageable Vehicles"      ]    }  }}var str1 = "Total Cost of Ownership";var str2 = "Total Cost of Ownership*Operational Cost*Asset Cost";console.log(getChildOf(str1));console.log(getChildOf(str2));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript