简体   繁体   English

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

[英]Access nested javascript object child properties with iterations

I have an object from which I want to access the child's of the properties我有一个 object 我想从中访问孩子的属性

 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" function getChildOf(x){ if(x.split("").includes("*")){ var temp = "obj" x.split("*").forEach((e,i,arr)=>{ temp = temp+"['"+e+"']" }); var final = temp; console.log(final) }else if(x=="Total Cost of Ownership"){ console.log(Object.keys(obj["Total Cost of Ownership"])); } } getChildOf(str1) getChildOf(str2)

I have strings according to which I want to return the object child's我有我想要返回 object 孩子的字符串

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

I wrote a function for it我为它写了一个 function

Now I wish the function should return the childs of the obj according to the function input, but I am not able to access it.现在我希望 function 应该根据 function 输入返回 obj 的孩子,但我无法访问它。 Request optimum solution?要求最佳解决方案?

Use this one用这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM