Iterating Complex data structure

Let say we have an array of authors

we can iterate all authors using any loop or for of here, but if we modify the data structure in below format,

then you cannot iterate all authors using for loop or any other loop with ease. So we can write our own method

The problem with this approrach is we need to be aware of the method called getAllAuthors and if our data sctructure has one more genre then we need to modify the getAllAuthors function as well. This is not cool.

So, what’s the other approach is make our data structure iterable. Can we do that. Yes we can!!

An iterable is a data structure that wants to make its elements accessible to the public. It does so by implementing a method whose key is Symbol.iterator. That method is a factory for iterators. That is, it will create iterators.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

One thought on “Iterating Complex data structure

  1. Object.keys(myFavouriteAuthors.allAuthors).map(item=>{
    myFavouriteAuthors.allAuthors[item].map(item=>console.log(item));
    })

Leave a Reply

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