jquery $.each() for objects
Asked Answered
S

5

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

Sidero answered 1/6, 2011 at 21:39 Comment(0)
T
147

$.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);
    });
});
Tightfisted answered 1/6, 2011 at 21:41 Comment(0)
K
15

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> 
Koger answered 1/6, 2011 at 21:42 Comment(2)
but then i don't get exact values just the index of the each object?Sidero
Because you're getting the whole Object here. To get exact values/extract values from Object : console.log(key +" "+ val.name + " " + val.price);Emotional
L
9

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

}
Lesslie answered 1/6, 2011 at 21:46 Comment(0)
L
3

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+"
"); }); });

Labrum answered 15/7, 2021 at 19:39 Comment(0)
R
2
var arType = [];
$.each($("input[name='archiveType[]']:checked"), function() {
   arType.push($(this).val());
});
arType = arType.join(",");
Riffraff answered 6/2, 2022 at 9:28 Comment(1)
Your answer could be improved by adding more information on what the code does and how it helps the OP.Compiler

© 2022 - 2024 — McMap. All rights reserved.