jquery each for objects

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

Feb 16 in Java by Rahul
• 9,680 points
82 views

1 answer to this question.

0 votes

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

});

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

answered Feb 16 by Aditya
• 7,660 points

Related Questions In Java

0 votes
1 answer

I am learning looping statements. Can you tell me how 'for-each' works in Java?

While programming we often write code that ...READ MORE

answered Apr 17, 2018 in Java by Rishabh
• 3,620 points
409 views
0 votes
1 answer

How does the Java 'for each' loop work?

for (Iterator<String> i = someIterable.iterator(); i.hasNext();) { ...READ MORE

answered Dec 22, 2020 in Java by Gitika
• 65,890 points
189 views
0 votes
0 answers

For-each over an array in JavaScript

How can I loop through all the ...READ MORE

Feb 8 in Java by Rahul
• 9,680 points
55 views
0 votes
1 answer

jQuery UI " $("#datepicker").datepicker is not a function"

I struggled with a similar problem for ...READ MORE

answered Feb 17 in Java by Soham
• 9,670 points
3,308 views
0 votes
1 answer

jquery $.each() for objects

each(), which is used to iterate, exclusively, over ...READ MORE

answered Jun 6 in JQuery by Edureka
• 13,640 points
57 views
0 votes
1 answer

Jquery - Using .each() with variables and an IF statement

To get values from elements, you need ...READ MORE

answered Jun 21 in JQuery by rajatha
• 7,480 points
339 views
0 votes
0 answers

jquery $.each() for objects

<script> $(document).ready(function() { ...READ MORE

Jul 20 in Web Development by gaurav
• 22,040 points
23 views
0 votes
0 answers

Jquery: Get each image src

I have a series of images each ...READ MORE

Aug 11 in Web Development by gaurav
• 22,040 points
923 views
0 votes
1 answer

jQuery fix for "Uncaught TypeError: $ is not a function" error

 Use the following lines of code in ...READ MORE

answered Feb 16 in Java by Aditya
• 7,660 points
317 views
0 votes
1 answer

nodemon command is not recognized in terminal for node js server

You need to install it globally npm install ...READ MORE

answered Feb 17 in Java by Aditya
• 7,660 points
866 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP