Skip to content

Instantly share code, notes, and snippets.

@julsfelic
Last active May 5, 2016 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julsfelic/ad061713b2fabe1796035006e82d3d5a to your computer and use it in GitHub Desktop.
Save julsfelic/ad061713b2fabe1796035006e82d3d5a to your computer and use it in GitHub Desktop.
Lololodash

Lololodash

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?

Getting Started

  • 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 });

Sort Me

  • sortBy takes a collection and a callback function or the .pluck callback shorthand.

Using a callback:

_.sortBy(collection, callback(value, index|key, collection);

Using pluck callback shorthand

_.sortBy(collection, 'age');

In Every Case

  • A lot of the ES5 functions are for either Arrays or Objects but can't be used for both. Lo-Dash functions can be!

Everyone Is Min

  • No notes taken

Chain Mail

  • _.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();

Count the Comments

  • No notes taken

Give Me an Overview

  • _.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)

Analyze

  • No notes taken

Start templating

  • Templating allows us to interpolate data into a string similar to the way we do with ERB
  • _.template('<b><%= value %></b>')({ value: 'attention' });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment