Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through nested objects in JS

Ok I'm stumped on this one. I need to iterate through these so I can make a listing by the category so something like

Business Books

Book 1

Book 2

Book 3

Cooking Books

etc..

But couldn't figure out how to iterate through the nested objects. With or without jquery is fine

window.books = {
    "Business Books": [
       {
           Title: "Finance 101",
           Description: "Info for Finance 101 book goes here."
       },
       {
           Title: "Economics 123",
           Description: "Info for Economics 123 book goes here."
       },
       {
           Title: "Statistics for Beginners",
           Description: "Learn about Statistics."
       }
    ],
    "Cooking Books": [
       {
           Title: "Lowfat Treats",
           Description: "Eat a lowfat Diet"
       },
       {
           Title: "Chocolate Lovers",
           Description: "Eat a lot of chocolate"
       },
       {
           Title: "Book of Brownies",
           Description: "Stuff about Brownies"
       }
    ],
    "IT Books": [
       {
           Title: "Windows XP",
           Description: "Please go away"
       },
       {
           Title: "Linux",
           Description: "A how to guide."
       },
       {
           Title: "Unix",
           Description: "All about Unix."
       },
       {
           Title: "Mac",
           Description: "Costs too much."
       }
    ],
};
like image 363
noober Avatar asked Jun 27 '12 16:06

noober


People also ask

How do I iterate through a nested array of objects in JavaScript?

You can create a function to loop through nested objects. That function automatically will check for nested objects and go through that objects. The for...in loop and Object. keys() method return keys/properties of the object.

Can you iterate through an object JavaScript?

There are two methods to iterate over an object which are discussed below: Method 1: Using for…in loop: The properties of the object can be iterated over using a for..in loop. This loop is used to iterate over all non-Symbol iterable properties of an object.


2 Answers

Good idea is to learn how to do it without jQuery first.

for(var category in window.books) {
  if(window.books.hasOwnProperty(category)) {
    console.log(category); // will log "Business Books" etc.
    for (var i = 0, j = window.books[category].length; i < j; i++) {
      console.log("Title: %s, Description: %s", window.books[category][i].Title, window.books[category][i].Description);
    }
  }
}

Then you can use $.each().

like image 121
mvbl fst Avatar answered Sep 21 '22 18:09

mvbl fst


$.each(window.books,function(k,v){   // k ==== key, v === value
        // Prints category
        console.log(k); 

        //Loops through category
        for(i=0,len=v.length;i<len;i++){
            console.log(v[i].Title);
            console.log(v[i].Description);
        }
    });
like image 24
Jashwant Avatar answered Sep 18 '22 18:09

Jashwant