📜  Lodash Lang 完整参考(1)

📅  最后修改于: 2023-12-03 15:02:47.399000             🧑  作者: Mango

Lodash Lang 完整参考

Lodash是一个JavaScript工具库,提供了许多实用的函数,可以帮助您提高代码开发效率。本文将为程序员介绍Lodash Lang,并提供丰富的内容。

安装

您可以使用npm来安装Lodash:

npm install lodash

或者您可以在HTML文件中使用CDN:

<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
使用

您可以使用以下方式来使用Lodash:

// 导入整个Lodash库
const _ = require('lodash');

// 导入Lodash的某个特定函数
const compact = require('lodash/compact');

// 通过全局对象(_)来使用Lodash
_.compact([0, 1, false, 2, '', 3]); // [1, 2, 3]
常用函数
数组函数

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

将一个数组分成指定大小的较小数组。

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

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

2. _.compact(array)

返回一个过滤掉所有falsey值的数组。

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

3. _.difference(array, [values])

返回一个过滤掉指定值后的数组。

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

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

返回一个将指定数量元素剔除后的新数组。

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

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

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

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

5. _.fill(array, value, [start=0], [end=array.length])

返回一个用指定值替换元素的数组。

_.fill([4, 6, 8, 10], '*', 1, 3);
// [4, '*', '*', 10]

6. _.flatten(array)

返回一个扁平化后的数组。

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

7. _.head(array)

返回数组中的第一个元素。

_.head([1, 2, 3]);
// 1

8. _.indexOf(array, value, [fromIndex=0])

返回一个给定值在数组中的第一个索引位置,如果找不到则返回-1。

_.indexOf([1, 2, 1, 2], 2);
// 1

_.indexOf([1, 2, 1, 2], 2, 2);
// 3

9. _.initial(array)

返回数组中除了最后一个元素以外的所有元素。

_.initial([1, 2, 3]);
// [1, 2]

10. _.intersection([arrays])

返回一个包含所有传递数组中所共有的元素的数组。

_.intersection([2, 1], [2, 3]);
// [2]

11. _.join(array, [separator=','])

将一个数组转换为字符串。可以指定分隔符。

_.join(['a', 'b', 'c'], '~');
// 'a~b~c'

12. _.last(array)

返回数组中的最后一个元素。

_.last([1, 2, 3]);
// 3

13. _.pull(array, [values])

从数组中剔除指定值后返回。

_.pull([1, 2, 3, 1, 2, 3], 2, 3);
// [1, 1]

14. _.reverse(array)

将数组翻转。

_.reverse([1, 2, 3]);
// [3, 2, 1]

15. _.slice(array, [start=0], [end=array.length])

返回一个从开始位置到结束位置的切片。

_.slice([1, 2, 3, 4], 1, 3);
// [2, 3]

16. _.sortedIndex(array, value)

返回按顺序插入值的索引。

_.sortedIndex([30, 50], 40);
// 1

17. _.unzip(array)

返回一个重新分组的数组。

_.unzip([['a', 'b'], [1, 2], [true, false]]);
// [['a', 1, true], ['b', 2, false]]
函数函数

1. _.after(n, func)

创建一个新函数,调用n次后才执行func函数。

const sayHello = () => console.log('Hello');
const newSayHello = _.after(2, sayHello);
newSayHello();
newSayHello();
// 输出 Hello

2. _.bind(func, thisArg, [partials])

创建一个新函数,并指定this,还可以提供部分参数。

const greet = function (greeting, punctuation) {
  return greeting + ' ' + this.user + punctuation;
};
const bound = _.bind(greet, { 'user': 'fred' }, 'hi');
bound('!');
// 'hi fred!'

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

返回一个节流型函数,只有在停止调用后指定的延迟时间后才执行。

const greet = () => console.log('Hello');
const newGreet = _.debounce(greet, 2000);
newGreet();
// 程序将在调用后两秒再执行

4. _.defer(func, [args])

将函数推入任务队列后立即返回。

_.defer(() => {
  console.log('deferred');
});
// 程序将在所有同步任务执行完毕后执行

5. _.flow([funcs])

创建一个组合函数,执行一系列函数并将每个函数的输出传递给下一个函数。

const awesome = fl.flow(func1, func2, func3);
awesome(input);

6. _.memoize(func, [resolver])

记忆化函数,缓存返回值,避免不必要的函数调用。

const multiply = (a, b) => a * b;
const memoized = _.memoize(multiply);
memoized(2, 5);
// 10

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

返回一个节流函数,会限制函数的调用频率。

const log = () => console.log('Hello');
const throttled = _.throttle(log, 1000);
throttled();

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

调用n次函数。

_.times(3, () => console.log('Hello'));
// 输出 Hello Hello Hello

9. .wrap(value, [wrapper=.identity])

包装一个值。

const greet = name => 'Hello ' + name + '!';
const wrapGreet = _.wrap(greet, function (func) {
  return 'I am ' + func('fred');
});
wrapGreet();
// 'I am Hello fred!'
集合函数

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

遍历collection中的每一个元素,使用iteratee函数处理,并返回collection。

_.forEach([1, 2, 3], value => console.log(value));
// 输出 1, 2, 3

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

将集合分组为多个组,由iteratee函数决定分组准则。

const users = [
  { 'user': 'fred', 'age': 48 },
  { 'user': 'barney', 'age': 36 },
  { 'user': 'fred', 'age': 42 },
  { 'user': 'barney', 'age': 34 }
];
_.groupBy(users, 'user');
// { 'fred': [{ 'user': 'fred', 'age': 48 }, { 'user': 'fred', 'age': 42 }],
//  'barney': [{ 'user': 'barney', 'age': 36 }, { 'user': 'barney', 'age': 34 }] }

3. _.size(collection)

返回集合的长度。

_.size([1, 2, 3]);
// 3
_.size({ 'a': 1, 'b': 2, 'c': 3 });
// 3
_.size('pebbles');
// 7
字符串函数

1. _.camelCase([string=''])

将字符串转换为驼峰命名法。

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

2. _.escape([string=''])

将字符串中的特殊字符转义。

_.escape('fred, barney, & pebbles');
// 'fred, barney, &amp; pebbles'

3. _.kebabCase([string=''])

将字符串转换为短横线分隔命名法。

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

4. _.lowerCase([string=''])

将字符串转换为小写。

_.lowerCase('--Foo-Bar--');
// 'foo bar'
_.lowerCase('fooBar');
// 'foo bar'
_.lowerCase('__FOO_BAR__');
// 'foo bar'

5. _.upperCase([string=''])

将字符串转换为大写。

_.upperCase('--foo-bar--');
// 'FOO BAR'
_.upperCase('fooBar');
// 'FOO BAR'
_.upperCase('__foo_bar__');
// 'FOO BAR'

6. _.repeat([string=''], [n=1])

重复字符串n次。

_.repeat('*', 3);
// '***'

7. _.snakeCase([string=''])

将字符串转换为蛇形命名法。

_.snakeCase('Foo Bar');
// 'foo_bar'

以上是Lodash Lang的部分常用函数。使用Lodash可以帮助您更快速地开发JavaScript应用。