1

Is there a way to save the "path" of a json object to a variable? That is, if I have something like this:

var obj = {"Mattress": {
                    "productDelivered": "Arranged by Retailer",
                    "productAge": {
                                "year": "0",
                                "month": "6"
                                }
                    }
       };

How can I loop through and save each key node name to a variable? eg. (I need it in this format): Mattress[productDelivered], Mattress[productAge][year], Mattress[productAge][month]

I have got partly there in this fiddle http://jsfiddle.net/4cEwf/ but as you can see in the log, year and month don't get separated but append to the array as well. I know this is because of the looping I have going on but I'm stuck on how to progress to get the data format I require. The flow I have set up in the fiddle is emulating what I need.

Is there a way I haven't considered to do this?

2 Answers 2

2

Try

var obj = {
    "Mattress": {
        "productDelivered": "Arranged by Retailer",
        "productAge": {
            "year": "0",
            "month": "6"
        }
    }
};

var array = [];

function process(obj, array, current){
    var ikey, value;
    for(key in obj){
        if(obj.hasOwnProperty(key)){
            value = obj[key];
            ikey = current ? current + '[' + key + ']' : key;
            if(typeof value == 'object'){
                process(value, array, ikey)
            } else {
                array.push(ikey)
            }
        }
    }
}
process(obj, array, '');
console.log(array)

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Wow! That's exactly what I was after :D Thank you so much :)
0
var obj = {"Mattress": {
                    "productDelivered": "Arranged by Retailer",
                    "productAge": {
                                "year": "0",
                                "month": "6"
                                }
                    }
       };
var Mattress = new Array();
for(var i in obj.Mattress){
    if(typeof(obj.Mattress[i])==='object'){
        for(var j in obj.Mattress[i]){
            if(Mattress[i]!=undefined){
                Mattress[i][j] = obj.Mattress[i][j];
            }
            else{
                Mattress[i] = new Array();
                Mattress[i][j] = obj.Mattress[i][j];
            }
        }
    }
    else{
        Mattress[i] = obj.Mattress[i];
    }
}     
for(var i in Mattress){
    if(typeof(Mattress[i])==='object'){
        for(var j in Mattress[i]){
            alert(j+":"+Mattress[i][j]);
        }
    }
    else{
        alert(i+":"+Mattress[i]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.