lodash 用法解释

539 阅读1分钟

每天学习五个lodash API,生活美滋滋。贵在坚持!

1、_.chunk(array,size) 第一个参数是原始数组,第二个参数是需要分割的尺寸,返回的是一个二维数组,以第二个参数分割

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

2、_.compact(array) ,移除所有的错误的成员(false, null, 0, "", undefined, and NaN ),返回移除后的数组

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

3、_.concat(array,[values]) 合并数组,第二个参数可以是单个参数,也可以是数组,但是二维数组只能展开一层。

eg: var other = _.concat(array, 2, [3], [[4]]);

console.log(other); // => [1, 2, 3, [4]]

4._.differrence([1,2],[2,3]) 接受两个数组,返回第一个数组里与第二个数组不一样的值

eg:

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

5._.drop(array,number) 第一个参数数原始数组,第二个参数是要截掉的位数(从数组的第一位开始,默认是一位),返回的截掉后剩下的元素

eg:

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