Array数组
- _.chunk(array, [size=1]) :将数组(array)拆分成多个 size 长度的区块,并将这些区块组成一个新数组。 如果array 无法被分割成全部等长的区块,那么最后剩余的元素将组成一个区块。
_.chunk(['a', 'b', 'c', 'd'], 3);
// => [['a', 'b', 'c'], ['d']]
- _.compact(array):创建一个新数组,包含原数组中所有的非假值元素。例如false, null, 0, “”, undefined, 和 NaN 都是被认为是“假值”。
_.compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]
- _.concat(array, [values]):创建一个新数组,将array与任何数组 或 值连接在一起。
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值的数组,每个值不包含在其他给定的数组中。
_.difference([3, 2, 1], [4, 2]);
// => [3, 1]
- _ .differenceWith(array, [values], [comparator]):这个方法类似_.difference ,除了它接受一个 comparator (注:比较器),它调用比较array,values中的元素。
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
_.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);
// => [{ 'x': 2, 'y': 1 }]
- _.drop(array, [n=1]):创建一个切片数组,去除array前面的n个元素。(n默认值为1。)
- _.dropRight(array, [n=1]) :去除array后面的n个元素。(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]
- _.fill(array, value, [start=0], [end=array.length]):使用 value 值来填充(替换) array,从start位置开始, 到end位置结束(但不包含end位置)。
var array = [1, 2, 3];
_.fill(array, 'a');
console.log(array);
// => ['a', 'a', 'a']
_.fill(Array(3), 2);
// => [2, 2, 2]
_.fill([4, 6, 8, 10], '*', 1, 3);
// => [4, '*', '*', 10]
- _.head(array):获取数组 array 的第一个元素。