Array แฃ Object แฃ String โ แแแแ แ แแแ แแแตแฐแณแฐแญ (manipulate แแแตแจแ)! JavaScript แแญ แจแแฐแแตแ แแแฎแฝ แ แแ แซแแแ!
debounce ยท throttle ยท cloneDeep ยท groupBy ยท merge โ แแแ แ Production แแแ แแธแ! โก
A modern JavaScript utility library delivering modularity, performance & extras.
Lodash (_) โ แ JavaScript แแตแฅ แจแแฐแแตแ แ แแ แแแญแฝแแฝ (utility functions) แ แแ แจแซแ แคแฐ-แแฝแแแต (library) แแ! Array แฃ Object แฃ String แฃ Function manipulation โ แแแ แซแ แฅแ แตแ
แฐแต แจแแแ แฎแต แฅแแตแแ
แ แญแจแณแแ! Tree-shakeable แตแแแ โ แจแแแแแแแ function แฅแป แแญแฐแ import แแตแจแ แฅแแฝแแแ!
Used by millions โ React, Angular, Vue, Node.js โ everywhere!
npm install lodash && npm install @types/lodash
import _ from 'lodash' | import { debounce } from 'lodash' (tree-shake!)
// Deep clone แแแตแจแ? JSON.parse(JSON.stringify(obj)) // โ Dates แฃ undefined แฃ Functions แญแ แแปแ! // Arrayแ แ แแตแฅ แแแจแแแ (Group)? arr.reduce((acc, item) => { acc[item.cat] = acc[item.cat] || []; acc[item.cat].push(item); return acc; }, {}) // โ 4 แแตแแญ แฎแต แ 1 แตแซ! // Debounce แแแตแจแ? let timer; clearTimeout(timer); timer = setTimeout(fn, 300);
import _ from 'lodash'; // โ Deep clone โ แแแแ แ แญแแต แณแณ (type) แญแฐแซแ! _.cloneDeep(obj) // โ Group โ แ 1 แแตแแญ แฅแป! _.groupBy(arr, 'category') // โ Debounce โ แ production แแแ แจแแ! _.debounce(fn, 300)
import { chunk, flatten, uniq, difference, intersection, compact, zip, unzip, groupBy, sortBy, orderBy } from 'lodash'; const arr = [1,2,3,4,5,6,7]; const nums = [1,2,2,3,3,4]; const nested = [[1,2],[3,4],[5]]; // chunk โ แตแแ arrayแ แแฐ แตแแแฝ chunks (แญแแแฝ) แแแจแแแ chunk(arr, 3) // [[1,2,3],[4,5,6],[7]] // flatten โ แจแฐแฐแซแจแ (nested) arrayแ แแฅ แแแตแจแ flatten(nested) // [1,2,3,4,5] flattenDeep([[1,[2]]]) // [1,2] (แแ แ แแ แแฅ แซแฐแญแแแ!) // uniq โ แจแฐแฐแแแ (duplicates) แณแณแแฝแ แแแตแแแต uniq(nums) // [1,2,3,4] uniqBy(users, 'id') // แ แฐแแฐแ property แฅแป แแฅแ แแแแฃแต // Set operations (แจแตแฅแตแฅ แตแแถแฝ) difference([1,2,3,4],[2,4]) // [1,3] intersection([1,2],[2,3]) // [2] union([1,2],[2,3]) // [1,2,3] // compact โ falsy แจแแ แฅแดแถแฝแ แแแตแแแต compact([0,1,false,2,'',3]) // [1,2,3] // zip / unzip zip(['a','b'],[1,2]) // [['a',1],['b',2]]
import { cloneDeep, merge, pick, omit, get, set, has, mapValues, invert, defaults } from 'lodash'; const user = { name:'Samuel', age:26, addr:{ city:'Addis', zip:'1000' } }; // cloneDeep โ แแ (Deep) Clone แแแตแจแ const clone = cloneDeep(user); clone.addr.city = 'Hawassa'; // แแแ (original) แณแณ แ แญแแจแญแ! // merge โ objects แแแแต (แ แฅแแแต / deep) merge({a:1,b:{x:1}}, {b:{y:2},c:3}) // โ {a:1, b:{x:1,y:2}, c:3} // pick / omit โ แจแฐแแฐแ fields แฅแป แแแแจแฅ pick(user, ['name','age']) // {name,age} แฅแป แญแฐแ แแ omit(user, ['addr']) // 'addr'แ แตแถ แแแแ แญแฐแ แแ // get โ safe nested access (แซแ แตแ แฐแต แณแณ แแแแฃแต) get(user, 'addr.city') // 'Addis' get(user, 'addr.phone', 'N/A') // แจแแ 'N/A' (default) แญแแแณแ has(user, 'addr.zip') // true // set โ nested value แแแตแแแฅ set(clone, 'addr.phone', '0911') // mapValues โ แ object แแตแฅ แซแแตแ แฅแดแถแฝ (values) แ แแ แแแแจแญ mapValues({a:1,b:2}, v => v*2) // โ {a:2, b:4}
import { camelCase, snakeCase, kebabCase, capitalize, startCase, truncate, pad, padStart, padEnd, trim, escape, unescape, words } from 'lodash'; const str = 'hello world ethiopia'; // ๐ช Case conversion (แจแ แแแ แตแแต แแแจแชแซ) camelCase(str) // 'helloWorldEthiopia' snakeCase(str) // 'hello_world_ethiopia' kebabCase(str) // 'hello-world-ethiopia' startCase(str) // 'Hello World Ethiopia' capitalize(str) // 'Hello world ethiopia' upperFirst(str) // 'Hello world ethiopia' // โ๏ธ truncate โ แจแ แ แ แแแ แแแณแ แญ (clip) truncate('Hello World Ethiopia!', { length: 12, omission: '...' // 'Hello Wor...' }) // ๐ pad โ string alignment (แญแแฐแถแฝแ แแแแแต) pad('abc', 8) // ' abc ' padStart('5', 4, '0') // '0005' (แ IDs แญแ แ แแ!) padEnd('ET', 5, '-') // 'ET---' // ๐ก๏ธ Security (แแฐแ แแแต) escape('<script>') // '<script>' words(str) // ['hello','world','ethiopia']
import { debounce, throttle, memoize, once, partial, curry } from 'lodash'; // โฑ๏ธ debounce โ แฐแ แแแ แ แ แฅแตแชแซแแ แญแ แฅแแ (แ Search input!) const search = debounce((query) => { fetchResults(query); // 300ms แซแแ แ แแ แฅแป แญแฐแซแ }, 300); input.addEventListener('input', (e) => search(e.target.value)); // ๐ฆ throttle โ แ แฐแแฐแ แแ แแตแฅ แ แแด แฅแป แฅแแฒแฐแซ (แ Scroll!) const onScroll = throttle(() => { updateProgress(); // แ แจ 200ms แ แแด แฅแป แญแฐแซแ }, 200); // ๐พ memoize โ แจแฐแซแแ แตแแต แแคแต แซแตแแแฃแ (Result Cache!) const factorial = memoize((n) => { return n <= 1 ? 1 : n * factorial(n - 1); }); factorial(100); // โ แแแฐแ แแ แฒแ แซ แแฅแณ แจ cache แญแฐแ แแ! // 1๏ธโฃ once โ แ แแด แฅแป แฅแแฒแฐแซ (แ Init functions!) const initApp = once(() => { console.log('App initialized!'); }); initApp(); // แญแฐแซแ (prints) initApp(); // โ แแ แญแแ โ แ แตแแ แ แญแฐแซแ!
import { groupBy, sortBy, orderBy, keyBy, countBy, sumBy, maxBy, minBy, filter, map, reduce, find } from 'lodash'; const products = [ { name:'Injera', cat:'food', price:50, rating:4.8 }, { name:'Tibs', cat:'food', price:200, rating:4.5 }, { name:'Coffee', cat:'drink', price:30, rating:4.9 }, { name:'Tej', cat:'drink', price:80, rating:4.2 }, ]; // groupBy โ แ Category แแแฐแฅ (แกแตแ แแตแจแ) groupBy(products, 'cat') // {food:[injera,tibs], drink:[coffee,tej]} // sortBy / orderBy โ แแฐแญแฐแญ sortBy(products, 'price') orderBy(products, ['rating'], ['desc']) // keyBy โ ID lookup map (แ แแฅแแต แณแณ แแแแแต) keyBy(products, 'name') // {Injera:{...}, Tibs:{...}} // Aggregation (แจแตแแต แตแซแแฝ) countBy(products, 'cat') // {food:2,drink:2} sumBy(products, 'price') // 360 maxBy(products, 'rating') // Coffee object minBy(products, 'price') // Coffee {price:30}
_.chain() โ แฅแ operationsแ แแฅ แ แตแญแ แซแซแญแแ! .value() แฒแฐแญแต แแฐแฑ แญแ แแแแ! แแฅแแแฐแ แณแณ แแฎแฐแฒแแ (Real-world data processing) แแญแก pattern แแ!
แแแแแ JS (ES2020+) แฅแ แจ Lodash features แ แซแฑ แ แซแทแ! แแแญ แแ แ แแแ Lodash แจแแซแธแแแฃแธแ แฆแณแแฝ แ แ!
| แฐแแฃแญ (Task) | โ Native JS (แจแฃแฅแ) | โ Lodash (แแแ) |
|---|---|---|
| Deep Clone | JSON.parse(JSON.stringify(obj)) โ Dates/undefined แญแ แแปแ! | _.cloneDeep(obj) โ แแแแ type แญแฐแซแ โ |
| Deep Merge | แฅแ recursive code แแแ แซแตแแแแ | _.merge(obj1, obj2) โ แ 1 แแตแแญ |
| Group By | arr.reduce((acc,x)=>{acc[x.k]=acc[x.k]||[];acc[x.k].push(x);return acc},{}) | _.groupBy(arr, 'key') โ แ 1 แแตแแญ! |
| Uniq Objects | Filter + JSON.stringify comparison | _.uniqBy(arr, 'id') โ แ property แฅแป |
| Debounce | Manual setTimeout/clearTimeout | _.debounce(fn, 300) โ แ production แแแ |
| Safe Get | obj?.a?.b?.c?.d (optional chain) | _.get(obj, 'a.b.c.d', default) |
| Sum By | arr.reduce((s,x)=>s+x.price, 0) | _.sumBy(arr, 'price') โ แแแ แฅแ แ แญแญ |
| Chunk | Custom loop + slice logic | _.chunk(arr, size) โ แ pagination! |
import { debounce, groupBy, orderBy, sumBy, maxBy, cloneDeep, pick, uniqBy, chunk, memoize } from 'lodash'; // 1๏ธโฃ Search โ แจ debounce แแญ (แ แแตแแแ API calls แญแแแณแ!) const handleSearch = debounce(async (query: string) => { const results = await searchProducts(query); setProducts(results); }, 350); // 2๏ธโฃ Dashboard analytics โ แแแ operations แฐแซแญแแ (chain)! const getDashboardData = memoize((orders: Order[]) => { const paid = orders.filter(o => o.status === 'paid'); return { byRegion: groupBy(paid, 'region'), totalRevenue:sumBy(paid, 'amount'), topOrder: maxBy(paid, 'amount'), topProducts: orderBy(paid, ['amount'],['desc']).slice(0,5), }; }); // memoize โ แฐแแณแณแญ input แฒแแฃ? cache แซแฐแญแแ โฉ // 3๏ธโฃ Safe user update โ แจ cloneDeep แแญ! const updateUser = (user: User, updates: Partial<User>) => { const safe = cloneDeep(user); // original แณแณแ แ แญแแซแ! return { ...safe, ...pick(updates, ['name','phone','address']) // แจแฐแแแฑ fields แฅแป }; }; // 4๏ธโฃ Pagination โ แจ chunk แแญ! const paginate = (items: any[], pageSize = 10) => chunk(items, pageSize); // paginate(products, 10) โ [[p1..p10],[p11..p20]...] // 5๏ธโฃ แจแฐแฐแแแ แฐแ แแแแฝแ (users) แ email แแแจแต const deduplicateUsers = (users: User[]) => uniqBy(users, 'email');
Search input โ debounce(fn, 300) โ แแฐแ แ แฐแแ แแฅแญ API แ แญแ แซแ! Scroll โ throttle(fn, 200) โ แ แฐแจแแต max 5 แแ! แ UI performance แแแ แ แตแแแ แแธแ!
JSON.parse(JSON.stringify()) Dates แฃ undefined แฃ Functions แซแ แแปแ! _.cloneDeep() แแ แแแแ type แ แตแญแญแ clone แซแฐแญแแ!
แ Dashboard แฃ Reports แฃ Charts แแ แซแแแแแ! groupBy(orders, 'region') + sumBy(group, 'amount') โ แแแ แจ analytics pipeline!
แฅแ operations แ แ
แฐแ แฐแจแฐแ chain แ แตแญแ! _.chain(data).filter().groupBy().mapValues().value() โ แแแแ แฅ แ แฃแ แแแ แแ!
import _ from 'lodash' = แแแแ แซแแฃแ (70KB)! import { debounce } from 'lodash' = แซแแ function แฅแป แซแแฃแ! แ Production แแแ named import แฐแ แแ!