Lodash数组方法中文总结

 

LodashAPI总结

Lodash是一个特别特别好用的工具,感觉有了Lodash就没有解决不了的问题了~~~~

使用初开始

官网 https://www.lodashjs.com/docs/4.17.5.html

安装    yarn add lodash    

           npm install lodash

在js文件中引用lodash:
const  _  =  require ( 'lodash')

Array Methods

  

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

   _.chunk ( arr ,num ) 用来 从前往后按num切割arr

官网上的例子

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

自己使用时的例子,将一个movieIds数组12个一组分组,从第10个开始往后

let movieIds = _.chunk(movieIds.slice(12), 10)

_.compact(array)

       用来去除集合中的无效元素,比如  false,    null,    0,    "",    undefined ,  NaN .等

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

_.concat(array, [values])

用来合并数组
var array = [1];
var other = _.concat(array, 2, [3], [[4]]);
 
console.log(other);
// => [1, 2, 3, [4]]
 
console.log(array);
// => [1]

    

_.difference(array, [values])

返回array内元素与values内的区别
_.difference([2, 1], [2, 3]);
// => [1]

。。。。。未完待续,有时间继续往上加

猜你喜欢

转载自www.cnblogs.com/katydids/p/9933157.html