當(dāng)前位置:首頁(yè)>生活>JavaScript lodash的常見(jiàn)用法
發(fā)布時(shí)間:2025-10-28閱讀(8)
|
Lodash是一個(gè)JavaScript的實(shí)用工具庫(kù),提供了很多常用的函數(shù)和工具,可以幫助我們更方便地操作數(shù)據(jù)和處理邏輯。本文將詳細(xì)介紹Lodash的常見(jiàn)用法,包括對(duì)象操作、數(shù)組操作、函數(shù)操作和其他常用操作等方面。
一、對(duì)象操作 1. _.get(object, path, [defaultValue]):獲取對(duì)象中指定路徑的值,如果路徑不存在則返回默認(rèn)值。 示例: const user = { name: 'John', address: { city: 'New York', street: '123 Main St' } }; _.get(user, 'address.city'); // 'New York' _.get(user, 'address.zip', 'N/A'); // 'N/A' 2. _.set(object, path, value):設(shè)置對(duì)象中指定路徑的值。 示例: const user = { name: 'John', address: { city: 'New York', street: '123 Main St' } }; _.set(user, 'address.city', 'Los Angeles'); console.log(user.address.city); // 'Los Angeles' 3. _.has(object, path):判斷對(duì)象中是否存在指定路徑的值。 示例: const user = { name: 'John', address: { city: 'New York', street: '123 Main St' } }; _.has(user, 'address.city'); // true _.has(user, 'address.zip'); // false 4. _.keys(object):獲取對(duì)象中所有的鍵名。 示例: const user = { name: 'John', age: 30, address: { city: 'New York', street: '123 Main St' } }; _.keys(user); // ['name', 'age', 'address'] 5. _.values(object):獲取對(duì)象中所有的鍵值。 示例: const user = { name: 'John', age: 30, address: { city: 'New York', street: '123 Main St' } }; _.values(user); // ['John', 30, {city: 'New York', street: '123 Main St'}]
二、數(shù)組操作 1. _.chunk(array, size):將數(shù)組按照指定大小分塊。 示例: const arr = [1, 2, 3, 4, 5, 6]; _.chunk(arr, 2); // [[1, 2], [3, 4], [5, 6]] _.chunk(arr, 3); // [[1, 2, 3], [4, 5, 6]] 2. _.compact(array):去除數(shù)組中的假值,包括false、null、0、''、undefined和NaN。 示例: const arr = [1, 0, false, '', null, undefined, NaN]; _.compact(arr); // [1] 3. _.difference(array, [values]):返回?cái)?shù)組中不包含在指定數(shù)組中的元素。 示例: const arr1 = [1, 2, 3, 4, 5]; const arr2 = [2, 4, 6]; _.difference(arr1, arr2); // [1, 3, 5] 4. _.drop(array, [n=1]):返回去除數(shù)組中前n個(gè)元素后的剩余元素。 示例: const arr = [1, 2, 3, 4, 5]; _.drop(arr); // [2, 3, 4, 5] _.drop(arr, 2); // [3, 4, 5] 5. _.take(array, [n=1]):返回?cái)?shù)組中前n個(gè)元素。 示例: const arr = [1, 2, 3, 4, 5]; _.take(arr); // [1] _.take(arr, 3); // [1, 2, 3]
三、函數(shù)操作 1. _.debounce(func, [wait=0], [options={}]):創(chuàng)建一個(gè)函數(shù),該函數(shù)在指定時(shí)間間隔內(nèi)只執(zhí)行一次。 示例: const debouncedFn = _.debounce(() => { console.log('Debounced function called'); }, 1000); debouncedFn(); // 調(diào)用后1秒才執(zhí)行 2. _.throttle(func, [wait=0], [options={}]):創(chuàng)建一個(gè)函數(shù),該函數(shù)在指定時(shí)間間隔內(nèi)最多執(zhí)行一次。 示例: const throttledFn = _.throttle(() => { console.log('Throttled function called'); }, 1000); throttledFn(); // 調(diào)用后立即執(zhí)行 setTimeout(() => { throttledFn(); // 1秒后執(zhí)行 }, 1000); 3. _.memoize(func, [resolver]):創(chuàng)建一個(gè)函數(shù),該函數(shù)緩存計(jì)算結(jié)果,避免重復(fù)計(jì)算。 示例: const fibonacci = _.memoize((n) => { if (n < 2) { return n; } return fibonacci(n - 1) fibonacci(n - 2); }); console.log(fibonacci(10)); // 55 console.log(fibonacci(10)); // 直接返回緩存的結(jié)果 四、其他常用操作 1. _.clone(value):復(fù)制一個(gè)值。 示例: const obj = {name: 'John'}; const clonedObj = _.clone(obj); console.log(clonedObj); // {name: 'John'} 2. _.isEqual(value, other):判斷兩個(gè)值是否相等。 示例: const obj1 = {name: 'John'}; const obj2 = {name: 'John'}; _.isEqual(obj1, obj2); // true 3. _.isEmpty(value):判斷一個(gè)值是否為空。 示例: _.isEmpty(null); // true _.isEmpty(undefined); // true _.isEmpty(''); // true _.isEmpty([]); // true _.isEmpty({}); // true 4. _.omit(object, [paths]):返回對(duì)象中除了指定鍵之外的所有鍵值對(duì)。 示例: const obj = {name: 'John', age: 30, address: '123 Main St'}; _.omit(obj, ['address']); // {name: 'John', age: 30} 5. _.pick(object, [paths]):返回對(duì)象中指定鍵的鍵值對(duì)。 示例: 6. _.random([lower=0], [upper=1], [floating]):返回指定范圍內(nèi)的隨機(jī)數(shù)。 示例: _.random(1, 10); // 生成1到10之間的整數(shù) _.random(1.5, 10.5, true); // 生成1.5到10.5之間的浮點(diǎn)數(shù) 7. _.template(string, [options={}]):編譯一個(gè)模板字符串,返回一個(gè)函數(shù),該函數(shù)可以根據(jù)傳入的數(shù)據(jù)生成最終的字符串。 示例: const compiled = _.template('Hello <%= name %>!'); console.log(compiled({name: 'John'})); // 'Hello John!' 8. _.truncate(string, [options={}]):截取字符串,如果超過(guò)指定長(zhǎng)度,則用...代替。 示例: const str = 'The quick brown fox jumps over the lazy dog'; _.truncate(str, {length: 20}); // 'The quick brown fox...' _.truncate(str, {length: 20, omission: ' (...continued)'}); // 'The quick brown fox (...continued)' 9. _.uniqueId([prefix='']):生成唯一的ID。 示例: _.uniqueId('user_'); // 'user_1' _.uniqueId('user_'); // 'user_2' 10. _.zip([arrays]):將多個(gè)數(shù)組合并成一個(gè)數(shù)組,每個(gè)元素為原數(shù)組中相同位置的元素組成的數(shù)組。 示例: const arr1 = [1, 2, 3]; const arr2 = ['a', 'b', 'c']; _.zip(arr1, arr2); // [[1, 'a'], [2, 'b'], [3, 'c']] 總結(jié) Lodash是一個(gè)非常實(shí)用的JavaScript工具庫(kù),提供了很多常用的函數(shù)和工具,可以大大提高開(kāi)發(fā)效率。本文介紹了Lodash的常見(jiàn)用法,包括對(duì)象操作、數(shù)組操作、函數(shù)操作和其他常用操作等方面,希望對(duì)大家有所幫助。 |
歡迎分享轉(zhuǎn)載→http://www.avcorse.com/read-731154.html
Copyright ? 2024 有趣生活 All Rights Reserve吉ICP備19000289號(hào)-5 TXT地圖HTML地圖XML地圖