T3.0
T3.0

Reputation: 496

get next item in json string array

I have a loop that loops through each item in an array and populates a check box.

the identity alternates throughout the loop.

"[{\"NAME\":\"TOM, B\",\"ID\":\"222\"},{\"NAME\":\"joe\",\"ID\":\"202\"}]"

how can I get the text of the select box to be the ID and the Name to be the text.

$.each($.parseJSON(json), function () {               
   $.each(this, function (key, value) {
     drname.options.add(new Option(value, value[+1])); 
    });
});

I tried the [+1] as I've seen in similar questions but no dice.

thanks.

Upvotes: 0

Views: 670

Answers (1)

Paul
Paul

Reputation: 141887

There is no need for that second $.each, just use the properties of the object directly without iterating through them:

$.each($.parseJSON(json), function(){               
    drname.options.add(new Option(this.ID, this.NAME)); 
});

Depending on what order the Option constructor expects the argument, it could also be:

new Option(this.NAME, this.ID)

Upvotes: 5

Related Questions