-1

I have multiple JSON objects which are have multiple JSON objects. Now how do i iterate the Objects in deep.

My JSON structure is like below.

{
  "phanim": {
    "msg1": {
      "data": "Hii ra..",
      "date": "Sep 9",
      "sender": "Phani",
      "type": "text"
    },
    "msg2": {
      "data": "How r u?",
      "date": "Sep 9",
      "sender": "Phani",
      "type": "text"
    }
  }
}
Teja Maridu
  • 515
  • 1
  • 6
  • 14

1 Answers1

0

Probably a recursive function - check if the typeof the current iterated property is an object, if so, iterate that!

var iterateObj = function(obj) {
    for (var key in obj) {
        if (typeof obj[key] === "object") {
            iterateObj(obj[key]);
        } else {
            console.log(obj[key]);
        }
    }
}

Note: this won't work if one of your properties is an array - you'll need an additional check and logic for that.

tymeJV
  • 103,943
  • 14
  • 161
  • 157