[Solved] – JavaScript – How do you iterate over a JSON structure?

How do you iterate over a JSON structure?

Asked on October 18, 2018 in Javascript.
Add Comment


  • 2 Answer(s)

         In this both the values are defined in array as well as in object the var the arr the values of array are stored and in the obj the values with number and it corresponding objects are defined with it.

     var arr = [ “one”, “two”, “three”, “four”, “five” ];
     var obj = { one:1, two:2, three:3, four:4, five:5 };

     jQuery.each(arr, function()

    {
     $(“#” + this).text(“My id is ” + this + “.”);
     return (this != “four”); // will stop running to skip “five”
    });

     jQuery.each(obj, function(i, val)

    {
     $(“#” + i).append(document.createTextNode(” – ” + val));
    });

    Answered on October 18, 2018.
    Add Comment

    Taken from jQuery docs:

    var arr = [ “one”, “two”, “three”, “four”, “five” ];
    var obj = { one:1, two:2, three:3, four:4, five:5 };

    jQuery.each(arr, function() {
    $(“#” + this).text(“My id is ” + this + “.”);
    return (this != “four”); // will stop running to skip “five”
    });

    jQuery.each(obj, function(i, val) {
    $(“#” + i).append(document.createTextNode(” – ” + val));
    });

    Answered on February 7, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.