Lo-Dash is a JavaScript library that gives us helpers for working with array, objects and JSON.
Personal question: When should we use Lo-Dash as opposed to ES5 / ES6 methods?
- To use Lo-Dash, we need to require it into our file:
var _ = require('lodash');
_.filter
takes a collection as its first argument and properties as is second. The properties is what we use to filter through the collection
_.filter(collection, { active: true });
sortBy
takes a collection and a callback function or the.pluck
callback shorthand.
_.sortBy(collection, callback(value, index|key, collection);
_.sortBy(collection, 'age');
- A lot of the ES5 functions are for either Arrays or Objects but can't be used for both. Lo-Dash functions can be!
- No notes taken
_.chain()
lets you chain or link several Lo-Dash methods together on a collection explicitly and then finally returns the value of the whole operation.- Example of chaining:
_.chain(words)
.sortBy()
.map(function(word) {
return (word + 'chained').toUpperCase();
})
.value();
- No notes taken
_.reduce()
is just slightly different in its syntax:
_.reduce(collection, [callback=identity], [accumulator]);
- The callback is also slightly different
callback(accumulator, value, index|key, collection)
- No notes taken
- Templating allows us to interpolate data into a string similar to the way we do with ERB
_.template('<b><%= value %></b>')({ value: 'attention' });