0

I have very complex json created at server side like -

{"2013": {"11": {"A": 0, "B": 310, "C": 6}, "12": {"A": 0, "B": 281, "C": 5}}, "2014": {"1": {"A": 0, "B": 310, "C": 6}}}

Above JSON object holds the values 'A','B', and 'C' for the three months i.e current + last 2months

var data = {{=monthly_result}}; //Python variable assigned to js var

Now I want to loop through the above object in javascript. Please guide me. Found many links but not any is fully helpful. Thanks in advance!

4
  • 1
    Google Javascript Loops Commented Jan 3, 2014 at 12:15
  • possible duplicate of How to read an object's properties while using a recursive function in Javascript? Commented Jan 3, 2014 at 12:16
  • Iterate and do what? Transform it into a new value, annotate objects with additional properties? Commented Jan 3, 2014 at 12:17
  • {{=monthly_result}}; ??? Commented Jan 4, 2014 at 4:27

3 Answers 3

1

You can use a for loop, here's a very simple example :

var data = {"2013": {"11": {"A": 0, "B": 310, "C": 6}, "12": 
           {"A": 0, "B": 281, "C": 5}}, "2014": {"1": 
           {"A": 0, "B": 310, "C": 6}}};

for(var year in data) {
    var ydata = data[year];
    for(var num in ydata) {
        var ndata = ydata[num];
        for(var l in ndata) {
            console.log(year + ' -> ' + num + ' -> ' + l + ' = ' + ndata[l]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. Its working...BUT when I am assigning my json to obj like - var obj = {{=monthly_result}} ......its giving some error. Any help around that Please.
@user3020422 instead of commenting here and there update your question with requirement
0

To iterate over a "complex" object in a generic way, I would proceed like this :

(psedo code to show how it could be done)

var result = [];
function objIterate(obj, i) {
   result.push([]);
   for (prop in obj){
      if (isObj(prop))
         objIterate(prop, i+1);
      else
         result[i].push(obj[prop]);
   }
}

var obj = yourJson;
objIterate(obj, 0);

Comments

0

Do something like this

var obj = {"2013": {
            "11": {"A": 0, "B": 310, "C": 6},
            "12": {"A": 0, "B": 281, "C": 5}
            }, 
          "2014": 
            {"1": {"A": 0, "B": 310, "C": 6}
           }
        };

for(outer in obj) {
    for (inner in obj[outer]) {
        for(innermost in obj[item][inner]) {
            alert(outer + "-->" + inner + "-->" + innermost + "-->" + obj[item][inner][innermost]);
        }
    }
}

2 Comments

Thank you so much. Its working...BUT when I am assigning my json to obj like - var obj = {{=monthly_result}} ......its giving some error. Any help around that Please.
@user3020422 can you please open a new question for this or just edit above..And if this is working please mark this as correct.Also check fiddle I also assigned json to variable obj

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.