Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over JSON object in jquery

Tags:

jquery

each

I have a json object as

[
{"DisplayName":"Answer Number 1","Value":"Answer1","Option":"True"},
{"DisplayName":"Answer Number 1","Value":"Answer1","Option":"False"},
{"DisplayName":"Answer Number 2","Value":"Answer2","Option":"True"},
{"DisplayName":"Answer Number 2","Value":"Answer2","Option":"False"}
]

What I need is to create 2 drop downs from this object as

Answer Number 1 -> True/False

Answer Number 2 -> True/False

dropdown part I'll do my self.. I m just confused on how to iterate over this object. Can any1 please lead me to some example?

like image 303
Gautam Avatar asked Jun 19 '13 08:06

Gautam


People also ask

How to loop json object in jQuery?

jQuery code snippet to loop through JSON data properties. You have an array of objects/maps so the outer loop loops through those. The inner loop loops through the properties on each object element.

How to loop through object in jQuery?

The $. each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.

How to loop json array in jQuery?

each() or $. each() Function. This is the simplest way of looping around an array or a JSON array in JavaScript or jQuery.

How to loop array data in jQuery?

forEach() , which uses the following syntax : myArray. forEach(function(value, key, myArray) { console. log(value); });


1 Answers

your json objects jsonObject are stored in an array. Do :

$.each(jsonArray, function(index,jsonObject){
    $.each(jsonObject, function(key,val){
        console.log("key : "+key+" ; value : "+val);
    });
});

it will gives you

key : DisplayName ; value : Answer Number 1
key : Value ; value : Answer 1
key : Option ; value : true

Anyway, Anthony is right. Your structure will be difficult to manipulate

like image 53
TCHdvlp Avatar answered Oct 13 '22 09:10

TCHdvlp