jquery $.each() for objects

JqueryEach

Jquery Problem Overview


<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?

Jquery Solutions


Solution 1 - Jquery

$.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);
    });
});

Solution 2 - Jquery

You are indeed passing the first data item to the each function.

Pass data.programs to the each function instead. Change the code to as below:

<script>     
	$(document).ready(function() {         
		var data = { "programs": [ { "name":"zonealarm", "price":"500" }, { "name":"kaspersky", "price":"200" } ] };         
		$.each(data.programs, function(key,val) {             
			alert(key+val);         
		});     
	}); 
</script> 

Solution 3 - Jquery

Basically you need to do two loops here. The one you are doing already is iterating each element in the 0th array element.

You have programs: [ {...}, {...} ] so programs[0] is { "name":"zonealarm", "price":"500" } So your loop is just going over that.

You could do an outer loop over the array

$.each(data.programs, function(index) {

    // then loop over the object elements
    $.each(data.programs[index], function(key, value) {
        console.log(key + ": " + value);
    }

}

Solution 4 - Jquery

Insert data in "INId" div...

$(document).ready(function() {
        var data = { "programs": [ { "name":"zonealarm", "price":"500" }, { "name":"kaspersky", "price":"200" } ] };
        $.each(data.programs, function(key,val) {
            // console.log(val);
            $('#InId').append("Name: " +val.name+" & Price: "+val.price+"<br/>");
        });
    });

$(document).ready(function() { var data = { "programs": [ { "name":"zonealarm", "price":"500" }, { "name":"kaspersky", "price":"200" } ] }; $.each(data.programs, function(key,val) { // console.log(val); $('#InId').append("Name: " +val.name+" & Price: "+val.price+"
"); }); });

Solution 5 - Jquery

var arType = [];
$.each($("input[name='archiveType[]']:checked"), function() {
   arType.push($(this).val());
});
arType = arType.join(",");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmatiasdelgadoView Question on Stackoverflow
Solution 1 - JqueryTomalakView Answer on Stackoverflow
Solution 2 - JqueryChanduView Answer on Stackoverflow
Solution 3 - JqueryMorgan ARR AllenView Answer on Stackoverflow
Solution 4 - JqueryAl-AminView Answer on Stackoverflow
Solution 5 - JqueryMohammad Ali AbdullahView Answer on Stackoverflow