Lodash 4 Cookbook - Leanpub

 Lodash 4 Cookbook

For lodash 4.17.10 Fu Cheng

This book is for sale at This version was published on 2022-01-31

This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. ? 2014 - 2022 Fu Cheng

Tweet This Book!

Please help Fu Cheng by spreading the word about this book on Twitter! The suggested hashtag for this book is #lodashcookbook. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: #lodashcookbook

Also By Fu Cheng

JUnit 5 Cookbook A Practical Guide for Java 8 Lambdas and Streams ES6 Generators From Java 11 to Java 17

To my wife Andrea and my daughters Olivia and Erica

Contents

1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.1 Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.1.1 Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1.1.2 NodeJS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1.2 Lodash features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.3 Code sample convention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.4 About this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2. Common concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.1 Truthy and falsy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.2 SameValueZero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.3 Predicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.3.1 matches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.3.2 matchesProperty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.3.3 property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 2.4 Iteratees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.4.1 Iteratee shorthand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.5 this binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

3. Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.1 Each . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.2 Every and some . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 3.3 Filter and reject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 3.4 Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 3.5 Includes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3.6 Sample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.7 Shuffle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 3.8 Partition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 3.9 Count by . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.10 Group by and key by . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3.11 invokeMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.12 Map and reduce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.12.1 Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.12.2 Reduce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

CONTENTS

3.13 Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.13.1 find . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.13.2 findLast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

3.14 Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.15 flatMap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

4. String Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 4.1 interpolate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 4.2 escape . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 4.3 evaluate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 4.4 imports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 4.5 Data object name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

5. Recipes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 5.1 Filter an object's properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 5.1.1 Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 5.1.2 Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 5.2 Push an array of elements into an array . . . . . . . . . . . . . . . . . . . . 36 5.2.1 Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 5.2.2 Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 5.3 Process data for C3.js pie chart . . . . . . . . . . . . . . . . . . . . . . . . . . 37 5.3.1 Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 5.3.2 Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 5.4 Create a unique array of objects . . . . . . . . . . . . . . . . . . . . . . . . . . 40 5.4.1 Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 5.4.2 Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 5.5 Convert an array to an object . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 5.5.1 Scenario . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 5.5.2 Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

6. Thank you . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

1. Introduction

This book is about the popular JavaScript utilities library lodash1. Before we discuss lodash, we should understand why we need a JavaScript utilities library. With the prevalence of Web 2.02, Ajax3 and NodeJS4, JavaScript has become a very important programming language in both browser-side and server-side. Besides the bad parts5 of JavaScript language, JavaScript itself doesn't have a rich set of high-level API for developers to use, which makes common programming tasks hard to complete.

For example, it's a very common task to iterate an array and process all elements in this array in sequence. In some old browsers, the JavaScript Array object doesn't have the method forEach(). To iterate an array, for loop is required as in Listing 1.1. process is the function to process elements in the array.

Listing 1.1 Traditional approach to iterate an array

for (var i = 0, n = array.length; i < n; i++) { process(array[i]);

}

When using the method forEach(), the code in Listing 1.1 can be simplified as in Listing 1.2.

Listing 1.2 Use forEach() to iterate an array

array.forEach(process);

Comparing code snippets in Listing 1.1 and Listing 1.2, it's obvious that Listing 1.2 is much simpler to understand and easier to write and maintain than the code in Listing 1.1. That's why developers want more high-level APIs. JavaScript itself is evolving to add more language features and APIs, but the process is not fast enough. ECMAScript6, the specification behind JavaScript, includes nine new methods for searching and manipulating array contents in 5th edition7. This means developers can use the method forEach() when the JavaScript engine supports ECMAScript

1 2 3(programming) 4 5Find out the good parts of JavaScript in Douglas Crockford's excellent book JavaScript: The Good Parts 6 7

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download