0

I am trying to loop through JSON data

{"reply":[{"ticketnumber":"61947"}],"all":[{"ticketnumber":"61947"}]);

using

success: function(data) {
    for(var i in data) {
        console.log(data[i]);
    }
}

but i cannot get the key values, so i want to get "reply" and "all" then loop through those inside the first loop

9
  • What is your expected output? Commented May 30, 2020 at 18:03
  • " i want to get "reply" and "all" then loop through those inside the first loop" Commented May 30, 2020 at 18:04
  • so a recursive loop? Commented May 30, 2020 at 18:04
  • Not getting what you mean. Commented May 30, 2020 at 18:04
  • well the json data returns data inside both "reply" and "all", but i want to display those values (reply and all) then loop the data inside "reply" and "all" Commented May 30, 2020 at 18:05

1 Answer 1

1

You can use Object.keys and then loop the data inside "reply" and "all" using for..of like:

const data = {
  "reply": [{
    "ticketnumber": "61947"
  }],
  "all": [{
    "ticketnumber": "61947"
  }]
};

for (var key of Object.keys(data)) {
  console.log(key);
  for (var i of data[key]) {
    console.log(i);
  }
}

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

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.