We use cookies to understand how the site is used and to display ads. Analytics and advertising only run after you accept. You can change your choice anytime. Privacy policy

Skip to content
devvkit
$devvkit learn --librarie lodash-guide

Lodash Guide

[utilities][data]
JavaScript / TypeScript
Install
npm install lodash
npm install -D @types/lodash

Lodash provides consistent, modular functions for common programming tasks.

Key utilities: cloneDeep, merge, debounce, groupBy, orderBy, keyBy.

Lodash v5 is tree-shakeable. Import only what you need.

Arrays

Chunk· Split array into groups.
import chunk from 'lodash/chunk';
chunk(['a','b','c','d'], 2); // [['a','b'],['c','d']]
Uniq by· Remove duplicates.
import uniqBy from 'lodash/uniqBy';
uniqBy([{id:1},{id:1},{id:2}], 'id');

Collections

Group by· Group by key.
import groupBy from 'lodash/groupBy';
groupBy([{role:'admin'},{role:'user'}], 'role');
Order by· Sort by criteria.
import orderBy from 'lodash/orderBy';
orderBy(users, ['age','name'], ['asc','desc']);

Objects

Pick· Select keys.
import pick from 'lodash/pick';
pick({name:'Alice', age:30, password:'secret'}, ['name','age']);
Omit· Exclude keys.
import omit from 'lodash/omit';
omit({name:'Alice', password:'secret'}, ['password']);

Functions

Debounce· Delay invocation.
import debounce from 'lodash/debounce';
const save = debounce((text) => api.save(text), 500);
Throttle· Limit invocation rate.
import throttle from 'lodash/throttle';
window.addEventListener('scroll', throttle(() => console.log('scroll'), 200));

Deep Operations

Clone deep· Deep clone with circular ref support.
import cloneDeep from 'lodash/cloneDeep';
const clone = cloneDeep(original);
clone.nested.a = 2; // original unchanged
Merge· Deep merge objects.
import merge from 'lodash/merge';
merge({ theme: { dark: false } }, { theme: { dark: true } });

Lang

Is equal· Deep comparison.
import isEqual from 'lodash/isEqual';
isEqual({a:1,b:{c:2}}, {a:1,b:{c:2}}); // true