Book Image

Learning Underscore.js

By : Alexandru Vasile Pop
Book Image

Learning Underscore.js

By: Alexandru Vasile Pop

Overview of this book

Table of Contents (14 chapters)
Learning Underscore.js
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Searching and filtering


In the previous chapter, we used the function _.find(list, predicate, [context]) to search for a specific item within a collection. This function is part of the Underscore comprehensive functionality for searching and filtering collections represented by object properties and array-like objects. We will make a distinction between search and filter functions with the former tasked with finding one item in a collection and the latter tasked with retrieving a subset of the collection (although sometimes, you will find the distinction between these functions thin and blurry).

We will revisit the find function and the other search-related and filtering-related functions using an example with slightly more diverse data that is suitable for database persistence. We will use the problem domain of a bicycle rental shop and build an array of bicycle objects with the following structure:

var getBicycles = function() {
  return [{
    id: 1,
    name: "A fast bike",
    type: ...