1. 程式人生 > >lodash官方文件總結

lodash官方文件總結

 

 

lodash官方中文文件:https://www.css88.com/doc/lodash/

Array

     _.chunk   (一個數組在內部分割出兩個陣列,可以指定第一個陣列的引數個數)

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

 _.compact (去除一個數組中的 0 false 和空值)

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

_.concat(將裡面的所有的數組合併到新的大陣列中,如果元素本身是陣列,則解除一層陣列,如果是正常數字則不動)

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

_.difference(將兩個作為引數的陣列比較,取出不相同的元素,組成新的陣列)

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

_.differenceWith(返回出前一個物件中和已有物件中不同的那個)

var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
 
_.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);
// => [{ 'x': 2, 'y': 1 }]

_.drop(從尾部刪除幾個元素,預設是一個) 

_.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]

_.dropRight(反過來)

_.dropRight([1, 2, 3]);
// => [1, 2]
 
_.dropRight([1, 2, 3], 2);
// => [1]
 
_.dropRight([1, 2, 3], 5);
// => []
 
_.dropRight([1, 2, 3], 0);
// => [1, 2, 3]