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

Testing truth conditions


Beyond simply filtering collections, you often need to test a condition of a collection. This could include filtering a collection, and then answering a simple yes/no about the results. In those cases where you need to check a truth condition of a collection, it's often easier to perform the test at the end of a chain. There's usually no need to write several statements and allocate several variables along the way.

Testing if a collection contains an item

Perhaps, the most straightforward test we can perform is whether or not a collection contains an item we're looking for. The contains() function is handy in cases like these because it is easy to attach to the end of a chain that's performing some other operations beforehand. One use of contains() is shown in the following example:

var string = 'abc123',
    array = [ 'a', 'b', 'c', 1, 2, 3 ];

_(string)
    .filter(_.isString)
    .contains('c');
// → true

_(array)
    .filter(_.isString)
    .contains('c');
// ...