logo
eng-flag

Lodash Cheatsheet

Lodash is a modern JavaScript utility library delivering modularity, performance & extras. This cheatsheet provides an overview of commonly used Lodash functions with examples.

Table of Contents

  1. Array
  2. Collection
  3. Function
  4. Lang
  5. Object
  6. String
  7. Utility

Array

_.chunk(array, [size=1])

Splits array into groups the length of size.

Example:

_.chunk(['a', 'b', 'c', 'd'], 2);
// => [['a', 'b'], ['c', 'd']]

_.chunk(['a', 'b', 'c', 'd'], 3);
// => [['a', 'b', 'c'], ['d']]

_.compact(array)

Removes falsey values from array.

Example:

_.compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]

_.difference(array, [values])

Creates an array of array values not included in the other given arrays.

Example:

_.difference([2, 1], [2, 3]);
// => [1]

_.drop(array, [n=1])

Creates a slice of array with n elements dropped from the beginning.

Example:

_.drop([1, 2, 3]);
// => [2, 3]

_.drop([1, 2, 3], 2);
// => [3]

_.flatten(array)

Flattens array a single level deep.

Example:

_.flatten([1, [2, [3, [4]], 5]]);
// => [1, 2, [3, [4]], 5]

_.flattenDeep(array)

Recursively flattens array.

Example:

_.flattenDeep([1, [2, [3, [4]], 5]]);
// => [1, 2, 3, 4, 5]

Collection

.countBy(collection, [iteratee=.identity])

Creates an object composed of keys generated from the results of running each element of collection thru iteratee.

Example:

_.countBy([6.1, 4.2, 6.3], Math.floor);
// => { '4': 1, '6': 2 }

.forEach(collection, [iteratee=.identity])

Iterates over elements of collection and invokes iteratee for each element.

Example:

_.forEach([1, 2], function(value) {
  console.log(value);
});
// => Logs `1` then `2`

.groupBy(collection, [iteratee=.identity])

Creates an object composed of keys generated from the results of running each element of collection thru iteratee.

Example:

_.groupBy([6.1, 4.2, 6.3], Math.floor);
// => { '4': [4.2], '6': [6.1, 6.3] }

.map(collection, [iteratee=.identity])

Creates an array of values by running each element in collection thru iteratee.

Example:

function square(n) {
  return n * n;
}

_.map([4, 8], square);
// => [16, 64]

Function

_.debounce(func, [wait=0], [options={}])

Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked.

Example:

const debounced = _.debounce(() => console.log('Hello'), 1000);
debounced();
debounced();
// => Logs 'Hello' after 1000ms

_.memoize(func, [resolver])

Creates a function that memoizes the result of func.

Example:

const fibonacci = _.memoize(n => {
  return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
});

fibonacci(10);
// => 55

_.throttle(func, [wait=0], [options={}])

Creates a throttled function that only invokes func at most once per every wait milliseconds.

Example:

const throttled = _.throttle(() => console.log('throttle'), 1000);
throttled();
throttled();
// => Logs 'throttle' once per 1000ms

Lang

_.isArray(value)

Checks if value is classified as an Array object.

Example:

_.isArray([1, 2, 3]);
// => true

_.isArray('abc');
// => false

_.isNumber(value)

Checks if value is classified as a Number primitive or object.

Example:

_.isNumber(3);
// => true

_.isNumber('3');
// => false

_.isString(value)

Checks if value is classified as a String primitive or object.

Example:

_.isString('abc');
// => true

_.isString(1);
// => false

Object

_.assign(object, [sources])

Assigns own enumerable string keyed properties of source objects to the destination object.

Example:

_.assign({ 'a': 1 }, { 'b': 2 }, { 'c': 3 });
// => { 'a': 1, 'b': 2, 'c': 3 }

_.get(object, path, [defaultValue])

Gets the value at path of object.

Example:

const object = { 'a': [{ 'b': { 'c': 3 } }] };

_.get(object, 'a[0].b.c');
// => 3

_.get(object, ['a', '0', 'b', 'c']);
// => 3

_.get(object, 'a.b.c', 'default');
// => 'default'

_.has(object, path)

Checks if path is a direct property of object.

Example:

const object = { 'a': { 'b': 2 } };

_.has(object, 'a');
// => true

_.has(object, 'a.b');
// => true

_.has(object, ['a', 'b']);
// => true

_.merge(object, [sources])

This method is like _.assign except that it recursively merges own and inherited enumerable string keyed properties of source objects into the destination object.

Example:

const object = {
  'a': [{ 'b': 2 }, { 'd': 4 }]
};

const other = {
  'a': [{ 'c': 3 }, { 'e': 5 }]
};

_.merge(object, other);
// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }

String

_.camelCase([string=''])

Converts string to camel case.

Example:

_.camelCase('Foo Bar');
// => 'fooBar'

_.camelCase('--foo-bar--');
// => 'fooBar'

_.camelCase('__FOO_BAR__');
// => 'fooBar'

_.kebabCase([string=''])

Converts string to kebab case.

Example:

_.kebabCase('Foo Bar');
// => 'foo-bar'

_.kebabCase('fooBar');
// => 'foo-bar'

_.kebabCase('__FOO_BAR__');
// => 'foo-bar'

_.trim([string=''], [chars=whitespace])

Removes leading and trailing whitespace or specified characters from string.

Example:

_.trim('  abc  ');
// => 'abc'

_.trim('-_-abc-_-', '_-');
// => 'abc'

Utility

.times(n, [iteratee=.identity])

Invokes the iteratee n times, returning an array of the results of each invocation.

Example:

_.times(3, String);
// => ['0', '1', '2']

_.times(4, () => 0);
// => [0, 0, 0, 0]

_.uniqueId([prefix=''])

Generates a unique ID. If prefix is given, the ID is appended to it.

Example:

_.uniqueId('contact_');
// => 'contact_104'

_.uniqueId();
// => '105'

_.range([start=0], end, [step=1])

Creates an array of numbers (positive and/or negative) progressing from start up to, but not including, end.

Example:

_.range(4);
// => [0, 1, 2, 3]

_.range(-4);
// => [0, -1, -2, -3]

_.range(1, 5);
// => [1, 2, 3, 4]

_.range(0, 20, 5);
// => [0, 5, 10, 15]

2024 © All rights reserved - buraxta.com