Categories
JavaScript

JavaScript looping through objects

Use the following example:

for (prop in google_news.sections) {
    console.log(“result:”,google_news.sections[prop].link);
  }

And the object:

var google_news = {
 sections : {
  ‘0’: {
    link : “this is the first link”,
    contents : “content for first link”
   },
  ‘1’: {
    link : “this is the second link”,
    contents : “content for second link”
   },
  ‘2’: {
    link : “this is the third link”,
    contents : “content for third link”
   },
  ‘3’: {
    link : “this is the four link”,
    contents : “content for four link”
   },
  ‘4’: {
    link : “this is the fifth link”,
    contents : “content for fifth link”
   }
 }
};

Leave a Reply

Your email address will not be published. Required fields are marked *