7 Examples of the Every Function in Underscore JS

7 Examples of the Every Function in Underscore JS

This tutorial takes a look at the _every() function in Underscore. When you need to apply a truth test to all values in an array, _every() is there to help you out. Because of JavaScript’s loose type checking, making use of _every() and the related _some() function can be helpful. _every() uses the signature of _.every(list, [predicate], [context]). The function will check each item of the given list and will return true if every item satisfies the predicate function or if list is null, undefined or empty.…

3 Examples of the Underscore Reduce Function

3 Examples of the Underscore Reduce Function

In Underscore, the _reduce() function is used to transform an array or object properties into one single value. The signature of the reduce function is _.reduce(list, iteratee, [memo], [context]). We can see that both memo, and context are optional parameters. If the memo parameter is not provided during function execution, then it is the first array element or object property that gets used for the seed value.…

Underscore JS sortBy Function

Underscore JS sortBy Function

This Underscore JS tutorial will focus on the Underscore sortBy function, which works with both JavaScript objects and JavaScript arrays. Like the other tutorials that offer code examples we can run, so too will we offer several examples of the sortBy() function here. We’ll look at sorting an integer array, sorting string arrays, sorting by the property name of an array of objects, as well as setting up custom sort criteria.…

How To Use The Find Function In Underscore JS

How To Use The Find Function In Underscore JS

The find() function is another very commonly used function in the Underscore JavaScript Library. The signature has a familiar look in _.find(list, predicate, [context]) similar to the some() function we recently covered. The find() function takes an array or object as the first parameter. The second parameter is a required function that must be passed which runs against each element in the array element or object property that was passed in as the first argument.…

Underscore JS Some Function

underscore js some function

In this ongoing look at Underscore JS, we’ll take a look at the some function. It bears a close resemblance to the find function. The some function has a signature of _.some(list, [predicate], [context]). What it does is to scan the list parameter for any items that contain a truthy value and return true if this is the case. This is the default behavior without passing the optional predicate function.…