<script>
$(document).ready(function() {
var data = { "programs": [ { "name":"zonealarm", "price":"500" }, { "name":"kaspersky", "price":"200" } ] };
$.each(data.programs[0], function(key,val) {
alert(key+val);
});
});
</script>
This code retrieves the first data. name:zonealarm
and price:500
.
How can I retrieve all the data in the object?
I tried $.each(data.programs, function(key,val)
but it didn't work.
Should I put it in a loop?
$.each()
works for objects and arrays both:
var data = { "programs": [ { "name":"zonealarm", "price":"500" }, { "name":"kaspersky", "price":"200" } ] };
$.each(data.programs, function (i) {
$.each(data.programs[i], function (key, val) {
alert(key + val);
});
});
...and since you will get the current array element as second argument:
$.each(data.programs, function (i, currProgram) {
$.each(currProgram, function (key, val) {
alert(key + val);
});
});