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
• 8,980 points
36 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,280 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
334 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,850 points
146 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
• 8,980 points
19 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
• 8,730 points
991 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
7 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
• 5,040 points
7 views
0 votes
1 answer

How to use jquery with asp.net ajax?

If you weren't aware, Microsoft is planning ...READ MORE

answered Oct 15, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
175 views
0 votes
1 answer

Is 'sparkline' a method?

I suggest you to check 2 things That jquery.sparkline.js is actually ...READ MORE

answered Nov 9, 2018 in Apache Spark by Frankie
• 9,830 points
576 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,280 points
94 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,280 points
35 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP