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?
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.
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.
each() or $. each() Function. This is the simplest way of looping around an array or a JSON array in JavaScript or jQuery.
forEach() , which uses the following syntax : myArray. forEach(function(value, key, myArray) { console. log(value); });
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With