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

Returning wrappers


Wrappers and the function call chains that follow don't exist randomly throughout our code. The next chapter covers this topic in more depth, so consider this as a teaser. So far, we've only looked at chains as they're constructed and executed in the same statement. However, if we're going through all the trouble of designing a call chain that serves a general purpose, wouldn't it be good not to keep assembling that chain? Let's design the chain with the following code:

function best(coll, prop, count) {
    return _(coll)
        .sortBy(prop)
        .takeRight(count);
}
                
var collection = [
    { name: 'Mathew', score: 92 },
    { name: 'Michele', score: 89 },
    { name: 'Joe', score: 74 },
    { name: 'Laurie', score: 83 }
];

var bestScore = best(collection, 'score', 2);

bestScore.value();
// → 
// [
//   { name: "Michele", score": 89 },
//   { name: "Mathew", score: 92 }
// ]

bestScore.reverse().value();
// → 
// [
//   { name: "Michele", score:...