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,670 points
310 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
674 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,910 points
382 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,670 points
216 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,700 points
5,850 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,670 points
317 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,640 points
2,874 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
150 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,071 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
1,683 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
7,063 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