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

Reducing objects


In this section, we will turn our attention to reducing objects and working with object accumulators. Reducing objects is a lot like reducing arrays, the difference being that you have a key instead of an index. Oh yeah, there's also the ordering, which is kind of important—arrays are ordered, objects aren't.

Previously in the chapter, we caught a glimpse of what accumulators are. Here we'll take a deeper look at object accumulators, including some of the built-in Lo-Dash functions that utilize this concept.

Reducing keys

You can reduce an object to something different based solely on its keys. For example, if there are only certain properties you need, you can reduce the object to include only those properties, using the following code:

var object = { 
        first: 'Kerry',
        last: 'Singleton',
        age: 41
    },  
    allowed = [ 'first', 'last' ];

_.reduce(object, function(result, value, key) {
    if (_.contains(allowed, key)) {
        result[key] = value;...