Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Learning UnderscoreJS
Learning UnderscoreJS

Learning UnderscoreJS: Explore the Underscore.js library by example using a test-driven development approach

eBook
$35.99 $39.99
Paperback
$39.19 $48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Learning UnderscoreJS

Chapter 2. Using Underscore.js with Collections

The previous chapter introduced Underscore and presented some of the principles and patterns that will help with using it more efficiently. The chapter concluded by setting up the development workflow used for the book examples.

In this chapter, we will explore Underscore functionality for collections using more in-depth examples. Some of the more advanced concepts related to Underscore functions such as scope resolution and execution context will be explained. The topics of the chapter are as follows:

  • Key Underscore functions revisited
  • Searching and filtering
  • Aggregations and transformations

This chapter assumes that you are familiar with JavaScript fundamentals such as prototypical inheritance and the built-in data types.

The source code for the examples from this chapter is hosted online at https://github.com/popalexandruvasile/underscorejs-examples/tree/master/collections, and you can execute the examples using the Cloud9 IDE at the...

Key Underscore functions revisited – each, map, and reduce

The previous chapter introduced key Underscore functions that are representative of the library and mentioned that some functions can be used with either an array or an object. This flexible approach means that some Underscore functions can operate over collections: an Underscore-specific term for arrays, array-like objects, and objects (where the collection represents the object properties). We will refer to the elements within these collections as collection items.

By providing functions that operate over object properties Underscore expands JavaScript reflection like capabilities. Reflection is a programming feature for examining the structure of a computer program, especially during program execution.

JavaScript is a dynamic language without static type system support (as of ES6). This makes it convenient to use a technique called duck typing when working with objects that share similar behaviors. Duck typing is a programming...

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",...

Aggregations and transformations

We dealt so far with functions tasked with extracting a value or a range of values from collections, and we will now move to functions that aggregate or transform collections. Aggregate functions operate over an entire collection to extract or calculate a single value or a limited set of values. Transformation functions operate over an entire collection and return a new collection that is usually a different representation of the original collection.

The first example of an aggregate function was _.reduce() and the first example of a transformation function was _.map(). The former calculates an accumulator value from the entire collection and the latter creates a new collection from the original collection.

Aggregations

We will use our example data for this chapter to revisit _.reduce() and define a new requirement to calculate the average rental price for all bicycles or for bicycles of a specific type. We will implement the getAverageRentalPrice(type) function...

Other collection-based functions

There are three more collection transformation functions that we will mention briefly here:

  • _.toArray(list) creates a native Array object from a collection.
  • _.shuffle(list) creates an array of randomized collection items using the Fisher-Yates shuffle algorithm.
  • _.sample(list, [n]) also creates an array of randomized collection items with an optional limit of n array items. If n is not specific, it returns one random item from the collection.

The last collection specific function for this chapter is _.invoke(list, methodName, *arguments) that will call method methodName for each collection item and will optionally pass an arguments list to the invoked method.

Summary

In this chapter, we explored many of the collection specific functions as provided by Underscore. We revisited some of the functions introduced in the previous chapter and demonstrated additional functionality. We continued with searching and filtering functions and finished with aggregation and transformation functions.

In the next chapter, we will demonstrate the Underscore features covering arrays, functions, and objects.

Left arrow icon Right arrow icon

Description

Underscore.js is one of the most popular modern JavaScript libraries used for functional programming. It can be used as a base for building complex JavaScript applications in a sustainable manner and for building other JavaScript libraries. It embraces functional programming principles but is not opinionated and can be used with imperative, object-oriented, functional, or other programming styles. This book explores how to use Underscore.js to power your code and understand modern JavaScript development concepts while applying a lightweight and efficient workflow to build applications. The book starts with an incremental Underscore.js introduction by exploring key JavaScript concepts. You will then explore the basic features of Underscore.js in action and establish a lightweight development workflow that allows the provided examples to be guided by tests. The book then covers the functionality of Underscore.js with in-depth examples and explanations for understanding and applying the Underscore.js API. You'll also learn how to use Underscore.js as a base for your own modules and libraries within an object-oriented or functional programming style, and will be able to explore Underscore.js use cases in different environments. Eventually, you'll learn about libraries that are closely related with Underscore.js, how to share code between client and server, and how to prepare for the upcoming JavaScript standard ECMAScript 6.

Who is this book for?

If you are a developer with fundamental JavaScript knowledge and want to use modern JavaScript libraries to extend your functional programming skills, then Underscore.js is an important library you should be familiar with.

What you will learn

  • Reference and call Underscore.js functions using a modern JavaScript development workflow
  • Apply Underscore.js to JavaScript arrays, objects, and functions
  • Take advantage of objectoriented or functional programming techniques with Underscore.js
  • Leverage Underscore.js to create code that targets client, server, or database contexts
  • Extend Underscore.js functionality with other closely related libraries
  • Reuse Underscore.jsbased code between client and server applications
  • Prepare for the upcoming JavaScript standard ECMAScript 6 and support older browsers
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Oct 30, 2015
Length: 224 pages
Edition : 1st
Language : English
ISBN-13 : 9781784393816
Languages :

What do you get with Print?

Product feature icon Instant access to your digital copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Redeem a companion digital copy on all Print orders
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Oct 30, 2015
Length: 224 pages
Edition : 1st
Language : English
ISBN-13 : 9781784393816
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 91.97 114.97 23.00 saved
React.js Essentials
$26.39 $32.99
Learning UnderscoreJS
$39.19 $48.99
Javascript Unlocked
$26.39 $32.99
Total $ 91.97 114.97 23.00 saved Stars icon

Table of Contents

8 Chapters
1. Getting Started with Underscore.js Chevron down icon Chevron up icon
2. Using Underscore.js with Collections Chevron down icon Chevron up icon
3. Using Underscore.js with Arrays, Objects, and Functions Chevron down icon Chevron up icon
4. Programming Paradigms with Underscore.js Chevron down icon Chevron up icon
5. Using Underscore.js in the Browser, on the Server, and with the Database Chevron down icon Chevron up icon
6. Related Underscore.js Libraries and ECMAScript Standards Chevron down icon Chevron up icon
7. Underscore.js Build Automation and Code Reusability Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.6
(5 Ratings)
5 star 60%
4 star 40%
3 star 0%
2 star 0%
1 star 0%
SuJo Dec 25, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book was very easy to digest and follow along, all of the information is organized rather well. I appreciated the paradigm on OOP programming, and I really can't fault the book much. It was probably just too short for my taste, and I would have loved to see an overall project worked during the course of the book. But I felt confident in reading and doing that I can put the puzzle pieces together for myself.Packt has a global $5 ebook sale right now so sorry Amazon I had to buy it there.
Amazon Verified review Amazon
ruben Dec 26, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Excelent resource Learning UnderscoreJSVery interesting book, I understand very simple this tool for programming the applications in JavaScript, the book is well-organizedin all the chapters. I cover the main points to start developing Software with this new library in Java Script software.The book starts with an incremental Underscore.js introduction by exploring key JavaScript concepts. You will then explore the basic features of Underscore.js in action and establish a lightweight development workflow that allows the provided examples to be guided by tests. The book then covers the functionality of Underscore.js with in-depth examples and explanations for understanding and applying the Underscore.js API. You'll also learn how to use Underscore.js as a base for your own modules and libraries within an object-oriented or functional programming style, and will be able to explore Underscore.js use cases in different environments.Ruben.
Amazon Verified review Amazon
Perry Nally Dec 30, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Learning Underscore.js is a perfect introduction to functional programming and ECMAScript 5. I learned faster through comparing what I already knew in regular Javascript with the same function using Underscore.js methods. Underscore.js is very powerful and I'm excited to make use of it in my own sites. Though ECMAScript 6 is almost out, you'll still be able to use Underscore.js, but look for the updated Underscore.js library at that time.I can see why frameworks that use Underscore.js, like Backbone.js, are so popular. Learning Underscore.js is an easy read with concepts plainly described for beginner and veteran js developers.
Amazon Verified review Amazon
Winston Dec 30, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
This book is not for the javascript NOOB. Underscore.js is a JavaScript library which provides utility functions and POWER for common programming tasks like those found in popular languages like Ruby. The author does a great job of explaining the Underscore library and its uses throughout the book. Readers will benefit from the authors explanation of Underscore.js in a test driven environment. Great book to get started.
Amazon Verified review Amazon
Dustin Marx Dec 16, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
"Learning Underscore.js" provides a comprehensive introduction to the JavaScript library Underscore.js. The book consists of seven chapters spanning approximately 200 substantive pages. The first few chapters introduce Underscore.js and cover the majority of its features. The fourth chapter looks at applying Underscore.js with object-oriented and functional paradigms and the fifth chapter describes applying Underscore in the contexts of web browser, server (Node.js), and database (MongoDB and PostgreSQL). The sixth chapter describes Underscore's relationship to other libraries such as Underscore-contrib and Babel and Underscore's relationship to the ECMAScript specification. The final chapter looks at using Underscore applications with other JavaScript tools and frameworks such as CommonJS, Browserify, Jasmine, and Gulp to build, deploy, and test Underscore applications."Learning Underscore.js" covers more than just Underscore.js. It also briefly describes lodash and it discusses the emerging ECMAScript 6 implementations and how Underscore pertains to ECMAScript 5 and ECMAScript 6 applications.There are numerous code listings in "Learning Underscore.js" that are black text on white background with no color syntax and no line numbers in the PDF version of the book Packt Publishing provided to me. I recommend downloading the code separately to use your favorite editor or IDE to view the code or use the Cloud9 links referenced in the book to view the code. The book provides numerous references to additional resources (many of which are freely available online) with additional background details and specific topics."Learning Underscore.js" is an introductory book that manages to provide a comprehensive introduction to Underscore.js and explains Underscore's relationship to the ECMAScript specifications and the future of JavaScript.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the digital copy I get with my Print order? Chevron down icon Chevron up icon

When you buy any Print edition of our Books, you can redeem (for free) the eBook edition of the Print Book you’ve purchased. This gives you instant access to your book when you make an order via PDF, EPUB or our online Reader experience.

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
Modal Close icon
Modal Close icon