Lodash is a modern JavaScript utility library delivering modularity, performance & extras. This cheatsheet provides an overview of commonly used Lodash functions with examples.
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']]
Removes falsey values from array.
Example:
_.compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]
Creates an array of array values not included in the other given arrays.
Example:
_.difference([2, 1], [2, 3]);
// => [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]
Flattens array a single level deep.
Example:
_.flatten([1, [2, [3, [4]], 5]]);
// => [1, 2, [3, [4]], 5]
Recursively flattens array.
Example:
_.flattenDeep([1, [2, [3, [4]], 5]]);
// => [1, 2, 3, 4, 5]
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 }
Iterates over elements of collection and invokes iteratee for each element.
Example:
_.forEach([1, 2], function(value) {
console.log(value);
});
// => Logs `1` then `2`
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] }
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]
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
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
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
Checks if value is classified as an Array object.
Example:
_.isArray([1, 2, 3]);
// => true
_.isArray('abc');
// => false
Checks if value is classified as a Number primitive or object.
Example:
_.isNumber(3);
// => true
_.isNumber('3');
// => false
Checks if value is classified as a String primitive or object.
Example:
_.isString('abc');
// => true
_.isString(1);
// => false
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 }
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'
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
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 }] }
Converts string to camel case.
Example:
_.camelCase('Foo Bar');
// => 'fooBar'
_.camelCase('--foo-bar--');
// => 'fooBar'
_.camelCase('__FOO_BAR__');
// => 'fooBar'
Converts string to kebab case.
Example:
_.kebabCase('Foo Bar');
// => 'foo-bar'
_.kebabCase('fooBar');
// => 'foo-bar'
_.kebabCase('__FOO_BAR__');
// => 'foo-bar'
Removes leading and trailing whitespace or specified characters from string.
Example:
_.trim(' abc ');
// => 'abc'
_.trim('-_-abc-_-', '_-');
// => 'abc'
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]
Generates a unique ID. If prefix is given, the ID is appended to it.
Example:
_.uniqueId('contact_');
// => 'contact_104'
_.uniqueId();
// => '105'
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