sakeferret
sakeferret

Reputation: 315

Use jQuery $.each() to iterate through object

Trying to iterate through and get key/value values from the following object:

var schedules = {
    "0": {
        "STAGE1": "1/2/13",
        "STAGE2": "2/12/13"
    },
    "1": {
        "STAGE1": "2/4/13",
        "STAGE2": "3/9/13"
    }
    "2": {
        "STAGE1": "4/13/13",
        "STAGE2": "5/21/13"
    }
}

with the following jQuery:

$.each(schedules, function(index) {

    $.each(schedules[index], function(key, value) {
        $("ul#list").append("<li>" + key + " " + value + "</li>");
    });

});

so that I end up with a simple list:

I don't seem to be using $.each() correctly... what am i doing wrong? Thx!

Upvotes: 3

Views: 13453

Answers (4)

Shaze
Shaze

Reputation: 987

Always validate your JSON schema from JSON formatter online to check your JSON arrays:

Looks like you were missing Comma. Expecting comma or }, not string.

https://jsonformatter.curiousconcept.com/#

Upvotes: 0

Jason P
Jason P

Reputation: 27022

Simple syntax error: http://jsfiddle.net/46bv5/

"1": {
    "STAGE1": "2/4/13",
    "STAGE2": "3/9/13"
}, //need a comma here
"2": {
    "STAGE1": "4/13/13",
    "STAGE2": "5/21/13"
}

Be sure to check your error console when something isn't working.

Upvotes: 10

Revanth kumar
Revanth kumar

Reputation: 98

yes there was syntax error please put a comma before "2"

"1": {
    "STAGE1": "2/4/13",
    "STAGE2": "3/9/13"
}, //here you have missed comma
"2": {

Upvotes: 1

Josh Leuah
Josh Leuah

Reputation: 19

You forgot a comma:

"1": {
    "STAGE1": "2/4/13",
    "STAGE2": "3/9/13"
}, // HERE

Upvotes: 1

Related Questions