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

Keys and values


Now it's time to turn our attention to object keys and values and how they can be used in function call chains. Often, these involve wrapping a plain object in a Lo-Dash instance and then using the keys() or values() functions to bootstrap the processing. There are also times when you have a collection of objects and you want to work with certain property values only. For this purpose, there are the pick() and omit() functions that can be exercised in chains.

Filtered keys and values

We can use the result of a filtered array of object keys at a later point in the chain. This comes in handy when we're not exactly sure which keys are available and we only have a best guess. Let's try filtering by keys and values:

var object = {
    firstName: 'Jerald',
    lastName: 'Wolfe',
    age: 49
};

_(object)
    .keys()
    .filter(function(item) {
        return (/name$/i).test(item);
    })
    .thru(function(items) {
        return _.at(object, items);
    })
    .value();
// → [ ...