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, 2022 in Java by Rahul
• 9,680 points
529 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, 2022 by Aditya
• 7,680 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
951 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,770 points
592 views
0 votes
0 answers

For-each over an array in JavaScript

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

Feb 8, 2022 in Java by Rahul
• 9,680 points
383 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, 2022 in Java by Soham
• 9,710 points
6,427 views
0 votes
1 answer

jquery $.each() for objects

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

answered Jun 6, 2022 in JQuery by Edureka
• 13,690 points
552 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, 2022 in JQuery by rajatha
• 7,680 points
3,365 views
0 votes
0 answers

jquery $.each() for objects

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

Jul 20, 2022 in Web Development by gaurav
• 23,260 points
302 views
0 votes
0 answers

Jquery: Get each image src

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

Aug 11, 2022 in Web Development by gaurav
• 23,260 points
3,730 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, 2022 in Java by Aditya
• 7,680 points
2,140 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, 2022 in Java by Aditya
• 7,680 points
8,776 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