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.
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'); // ...