Book Image

Lo-Dash Essentials

By : Adam Boduch
Book Image

Lo-Dash Essentials

By: Adam Boduch

Overview of this book

Table of Contents (15 chapters)
Lo-Dash Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Transformations


Now it's time for us to look at transformations that happen to data as it passes through the processing pipelines we construct. What's interesting about Lo-Dash and how it transforms data in chains is that the original collection isn't modified—a new one is constructed. This reduces side effects and is fundamental to the immutability concept in other functional programming languages.

Building groups, unions, and unique values

Some of the most powerful transformation tools found in Lo-Dash can be used out of the box with very little effort. These include grouping collection items by a specific value they contain, joining arrays together while retaining only unique values, and removing any duplicates from arrays. For example:

var collection = [
    { name: 'Rudolph', age: 24 },
    { name: 'Charles', age: 43 },
    { name: 'Rodney', age: 37 },
    { name: 'Marie', age: 28 }
];
    
_(collection)
    .map(function(item) {
        var experience = 'seasoned veteran';
        if...