Solved

Looping thru json

Posted on 2014-03-26
2
116 Views
Last Modified: 2014-04-07
In JavaScript how would you loop thru this json structure and output the values of dbname and target.

var object = {
db1 : {dbname : 'xxxxxx',target : '100.0'},
db2 : {dbname : 'yyyyyyy',target : '200.0'},
db3 : {dbname : 'zzzzzz',target : '500.0'}
}

Open in new window

0
Comment
Question by:stargateatlantis
2 Comments
 
LVL 5

Assisted Solution

by:jayakrishnabh
jayakrishnabh earned 250 total points
ID: 39956476
function abc(){
var object = {
db1 : {dbname : 'xxxxxx',target : '100.0'},
db2 : {dbname : 'yyyyyyy',target : '200.0'},
db3 : {dbname : 'zzzzzz',target : '500.0'}
};

$.each(object, function (j, item) {
                alert(object[j].dbname);
                alert(object[j].target);
            });
}
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 39956544
Test page : http://jsfiddle.net/EJPP3/

var object = {
    db1 : {dbname : 'xxxxxx',target : '100.0'},
    db2 : {dbname : 'yyyyyyy',target : '200.0'},
    db3 : {dbname : 'zzzzzz',target : '500.0'}
}

for(var k in object) {
    var dbname = object[k].dbname;
    var target = object[k].target;
    //console.log(dbname + " / " + target);
    alert(dbname + " / " + target);
}

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now