1 回答
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));
添加回答
举报