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

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

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

慕田峪7331174 2021-10-29 17:06:32
我有一个对象,我想从中访问子级的属性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 的孩子,但我无法访问它。请求最佳解决方案?
查看完整描述

1 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

用这个


function getChildOf(x){

  var keys = x.split("*")

  let tempObj = obj;

  for (const key of keys) {

        tempObj = tempObj[key]

    }

    return tempObj;

}


// Test with your data

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


console.log(getChildOf(str1));

console.log(getChildOf(str2));



查看完整回答
反对 回复 2021-10-29
  • 1 回答
  • 0 关注
  • 138 浏览
慕课专栏
更多

添加回答

举报

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