Lodash Tips and Tricks

ES6

  Highcharts.charts
    .filter( c => c && isVisible(c.container))
    .forEach(c => console.log(c));

Lodash

When using Lodash chaining, you need to be careful to call value() at the end!

https://lodash.com/docs#_

Creates a lodash object which wraps value to enable implicit chaining. Methods that operate on and return arrays, collections, and functions can be chained together. Methods that retrieve a single value or may return a primitive value will automatically end the chain returning the unwrapped value. Explicit chaining may be enabled using _.chain. The execution of chained methods is lazy, that is, execution is deferred until _#value is implicitly or explicitly called.

Lazy evaluation allows several methods to support shortcut fusion. Shortcut fusion is an optimization strategy which merge iteratee calls; this can help to avoid the creation of intermediate data structures and greatly reduce the number of iteratee executions.

For example, it would be easy to forget the .value(), when comparing to the equivalent ES6 version.

  _(Highcharts.charts)
    .filter( c => c)
    .filter( c => isVisible(c.container))
    .forEach(c => console.log(c))
    .value(); // Needed to tell lodash to actually run the code