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.…

Underscore JS Each Function

Underscore JS Each Function

As we mentioned, the various collection functions in Underscore really help us to navigate our way around JavaScript objects and arrays in a really easy and straightforward way. In this particular tutorial we’ll have a look at the each function and how we can use it with arrays, objects, arrays with context, as well as objects with context. When we say with context, all we really mean is what the this keyword will be pointing to during particular portions of code execution.…