Post/Code

HomeAboutUsesNow

Lodash Notes.

Lodash is a great utility library for working with data in JavaScript. Combined ES6 functionality, Lodash really helps in making your code simpler and easier to read. It does add a dependency, but my feeling is that the efficiency and effectivness of using it makes up for the time and effort one would spend implementing similar functionality on your own. Of course, writing custom functions will help you learn and improve your coding ability, one needs to evaluate the pros and cons of your particular context.

Here are a few useful Lodash notes:

  • Installation: npm i lodash
  • Node.js usage: const _ = require('lodash');
  • React usage: import _ from 'lodash';

Useful Lodash functions:

_.pick

var object = { 'a': 1, 'b': '2', 'c': 3 };
_.pick(object, ['a', 'c']);
// { 'a': 1, 'c': 3 }

_.pickBy

var object = { 'a': 1, 'b': '2', 'c': 3 }; 
_.pickBy(object, 1);
// { 'a': 1 }

_.includes

_.includes([1, 2, 3], 1);
// true

_.debounce

_.debounce(yourFunctionHere, 500)
// executes function after 500ms

_.without

_.without([2, 1, 4, 3], 1, 2);
// [4, 3]