Search code examples
javascriptobjectjavascript-objects

Access nested javascript object child properties with iterations


I have an object from which I want to access the child's of the properties

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

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

I wrote a function for it

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. Request optimum solution?


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