Getting the book list from tag

We do the intersection based only on ISBN because the simple string value can make best use of the Lo-Dash’s methods.

1data.findBooksByTags = function(tags) {
2  var isbns = this.allIsbns(); // all books by default
3  _.forEach(tags, function(tag){ // loop all tags
4    var isbnsFromTag ="tag:" + tag); // get the ISBN list from the tag
5    isbns = _.intersection(isbns, isbnsFromTag); // intersect with the result.
6  });
7  return this.findBooksByIsbns(isbns); // get back the book list from ISBNs.

We need an array of all book ISBNs. This can be done by list all books and retrieve the ISBN in each book. You may think of a for-loop statement, but the map function can help.

1// return all ISBNs of each book in book list.
2data.allIsbns = function() {
3  return, function(book){
4    return book.isbn;
5  })

Since the collection manipulation is based on ISBNs, the last thing we need to do is retrieve the book list from the ISBNs.

 1// isbns should be array
 2data.findBooksByIsbns = function(isbns) {
 3  var result = [];
 4  for(var i=0, len=data.books.length; i<len; i++) {
 5    var book = data.books[i];
 6    if (_.contains(isbns, book.isbn)) {
 7      result.push(book);
 8    }
 9  }
10  return result;

What’s next? We’re going to take a look at “Render book list with argument”.

overlaied image when clicked on thumbnail

Makzan | Mobile web app dev with phonegap | Table of Content