Lodash教程--(2)数组Array

Lodash教程--(2)数组Array

  • 欢迎来到Lodash教程--(2)数组Array
    • (1) _.chunk(array, [size=1])
    • (2)_.compact(array)
    • (3)_.concat(array, [values])
    • (4)_.difference(array, [values])
    • (5)_.differenceBy(array, [values], [iteratee= _.identity])
    • (6)_.differenceWith(array, [values], [comparator])
    • (7)_.drop(array, [n=1])
    • (8)_.dropRight(array, [n=1])
    • (9) _.dropRightWhile(array, [predicate= _.identity])
    • (10) _.dropWhile(array, [predicate= _.identity])
    • (11)_.fill(array, value, [start=0], [end=array.length])
    • (12) _.findIndex(array, [predicate= _.identity], [fromIndex=0])
    • (13) _.findLastIndex(array, [predicate= _.identity], [fromIndex=array.length-1])
    • (14) _.flatten(array)
    • (15)_.flattenDeep(array)
    • (16)_.flattenDepth(array, [depth=1])
    • (17)_.fromPairs(pairs)
    • (18)_.head(array)
    • (19)_.indexOf(array, value, [fromIndex=0])
    • (20)_.initial(array)
    • (21)_.intersection([arrays])
    • (22) _.intersectionBy([arrays], [iteratee= _.identity])
    • (23)_.intersectionWith([arrays], [comparator])
    • (24)_.join(array, [separator=','])
    • (25)_.last(array)
    • (26)_.lastIndexOf(array, value, [fromIndex=array.length-1])
    • (27) _.nth(array, [n=0])
    • (28)_.pull(array, [values])
    • (29)_.pullAll(array, values)
    • (30) _.pullAllBy(array, values, [iteratee= _.identity])
    • (31)_.pullAllWith(array, values, [comparator])
    • (32)_.pullAt(array, [indexes])
    • (33) _.remove(array, [predicate= _.identity])
    • (34)_.reverse(array)
    • (35)_.slice(array, [start=0], [end=array.length])
    • (36) _.sortedIndex(array, value)
    • (37) _.sortedIndexBy(array, value, [iteratee= _.identity])
    • (38)_.sortedLastIndexOf(array, value)
    • (39)_.sortedUniq(array)
    • (40)_.sortedUniqBy(array, [iteratee])
    • (41)_.tail(array)
    • (42)_.take(array, [n=1])
    • (43)_.takeRight(array, [n=1])
    • (44) _.takeRightWhile(array, [predicate= _.identity])
    • (45)_.takeWhile(array, [predicate= _.identity])
    • (46)_.union([arrays])
    • (47) _.unionBy([arrays], [iteratee= _.identity])
    • (48)_.unionWith([arrays], [comparator])
    • (49)_.uniq(array)
    • (50)_.uniqBy(array, [iteratee= _.identity])
    • (51)_.uniqWith(array, [comparator])
    • (52)_.unzip(array)
    • (53)_.unzipWith(array, [iteratee= _.identity])
    • (54)_.without(array, [values])
    • (55)_.xor([arrays])
    • (56)_.xorBy([arrays], [iteratee= _.identity])
    • (57)_.xorWith([arrays], [comparator])
    • (58)_.zip([arrays])
    • (59)_.zipObject([props=[]], [values=[]])
    • (60)_.zipObjectDeep([props=[]], [values=[]])
    • (61)_.zipWith([arrays], [iteratee= _.identity])

欢迎来到Lodash教程–(2)数组Array

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

  • 规则:将数组(array)拆分成多个 size 长度的区块,并将这些区块组成一个新数组。 如果array 无法被分割成全部等长的区块,那么最后剩余的元素将组成一个区块。
  • array (Array): 需要处理的数组
  • [size=1] (number): 每个数组区块的长度
_.chunk(['a', 'b', 'c', 'd'], 2);
// => [['a', 'b'], ['c', 'd']]
 
_.chunk(['a', 'b', 'c', 'd'], 3);
// => [['a', 'b', 'c'], ['d']]

(2)_.compact(array)

  • 创建一个新数组,包含原数组中所有的非假值元素。
  • 假值:false, null, 0, "", undefined, 和 NaN
  • array (Array): 待处理的数组。
_.compact([0, 1, false, 2, '', 3, NaN, null]);
// => [1, 2, 3]

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

  • 创建一个新数组,将array与任何数组或值连接在一起。
  • array (Array): 被连接的数组。
  • [values] (…*): 连接的值。
var array = [1];
_.concat(array, 2, [3], [[4]], [[1], [2]]);
//=>[1,2,3,[4],[1],[2]]

_.concat(array, 2, [3], [[4]], '123');
//=>[1, 2, 3,[4], "123"]

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

  • array (Array): 要检查的数组。
  • [values] (…Array): 排除的值。
_.difference([3, 2, 1], [4, 2]);
// => [3, 1]

(5)_.differenceBy(array, [values], [iteratee= _.identity])

  • 这个方法类似_.difference ,除了它接受一个 iteratee (迭代器), 调用array 和 values 中的每个元素以产生比较的标准。 结果值是从第一数组中选择。iteratee 会调用一个参数:(value)。
_.differenceBy([3.1, 2.2, 1.3], [4.4, 2.5], Math.floor);
// => [3.1, 1.3]
 
// The `_.property` iteratee shorthand.
_.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');
// => [{ 'x': 2 }]
  • 第一个例子是将所有元素向下取整然后再进行比较。
  • 第二个例子是将x作为主key,如果x后面的值相同则去掉。

(6)_.differenceWith(array, [values], [comparator])

  • 它调用比较arrayvalues中的元素。 结果值是从第一数组中选择。comparator 调用参数有两个:(arrVal, othVal)
  • array (Array): 要检查的数组。
  • [values] (…Array): 排除的值。
  • [comparator] (Function): comparator 调用每个元素。
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
 
_.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);
// => [{ 'x': 2, 'y': 1 }]

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

  • 创建一个切片数组,去除array前面的n个元素。(n默认值为1。)
  • 【注】从1开始数,不是从0开始。
_.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]

(8)_.dropRight(array, [n=1])

  • 创建一个切片数组,去除array尾部的n个元素。(n默认值为1。)
  • array (Array): 要查询的数组。
  • [n=1] (number): 要去除的元素个数。
_.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]

(9) _.dropRightWhile(array, [predicate= _.identity])

  • 创建一个切片数组,去除array中从 predicate 返回假值开始到尾部的部分。predicate 会传入3个参数: (value, index, array)。
  • array (Array): 要查询的数组。
  • [predicate=_.identity] (Function): 这个函数会在每一次迭代调用。
var users = [
  { 'user': 'barney',  'active': true },
  { 'user': 'fred',    'active': false },
  { 'user': 'pebbles', 'active': false }
];
 
_.dropRightWhile(users, function(o) { return !o.active; });
// => objects for ['barney']
 
// The `_.matches` iteratee shorthand.
_.dropRightWhile(users, { 'user': 'pebbles', 'active': false });
// => objects for ['barney', 'fred']
 
// The `_.matchesProperty` iteratee shorthand.
_.dropRightWhile(users, ['active', false]);
// => objects for ['barney']
 
// The `_.property` iteratee shorthand.
_.dropRightWhile(users, 'active');
// => objects for ['barney', 'fred', 'pebbles']

(10) _.dropWhile(array, [predicate= _.identity])

  • 创建一个切片数组,去除array中从起点开始到 predicate 返回假值结束部分。predicate 会传入3个参数: (value, index, array)。
  • array (Array): 要查询的数组。
  • [predicate=_.identity] (Function): 这个函数会在每一次迭代调用。
  • (Array): 返回array剩余切片。
var users = [
  { 'user': 'barney',  'active': false },
  { 'user': 'fred',    'active': false },
  { 'user': 'pebbles', 'active': true }
];
 
_.dropWhile(users, function(o) { return !o.active; });
// => objects for ['pebbles']
 
// The `_.matches` iteratee shorthand.
_.dropWhile(users, { 'user': 'barney', 'active': false });
// => objects for ['fred', 'pebbles']
 
// The `_.matchesProperty` iteratee shorthand.
_.dropWhile(users, ['active', false]);
// => objects for ['pebbles']
 
// The `_.property` iteratee shorthand.
_.dropWhile(users, 'active');
// => objects for ['barney', 'fred', 'pebbles']

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

  • 使用 value 值来填充(替换) array,从start位置开始, 到end位置结束(但不包含end位置)
  • 这个方法会改变array而不是创建新数组。
  • array (Array): 要填充改变的数组。
  • value (*): 填充给 array 的值。
  • [start=0] (number): 开始位置(默认0)。
  • [end=array.length] (number):结束位置(默认array.length)。
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]

(12) _.findIndex(array, [predicate= _.identity], [fromIndex=0])

  • 返回第一个通过 predicate 判断为真值的元素的索引值(index),而不是元素本身。
  • array (Array): 要搜索的数组。
  • [predicate=_.identity] (Array|Function|Object|string): 这个函数会在每一次迭代调用。
  • [fromIndex=0] (number): The index to search from.
var users = [
  { 'user': 'barney',  'active': false },
  { 'user': 'fred',    'active': false },
  { 'user': 'pebbles', 'active': true }
];
 
_.findIndex(users, function(o) { return o.user == 'barney'; });
// => 0
 
// The `_.matches` iteratee shorthand.
_.findIndex(users, { 'user': 'fred', 'active': false });
// => 1
 
// The `_.matchesProperty` iteratee shorthand.
_.findIndex(users, ['active', false]);
// => 0
 
// The `_.property` iteratee shorthand.
_.findIndex(users, 'active');
// => 2

(13) _.findLastIndex(array, [predicate= _.identity], [fromIndex=array.length-1])

  • 这个方式类似 _.findIndex, 区别是它是从右到左的迭代集合array中的元素。
  • array (Array): 要搜索的数组。
  • [predicate=_.identity] (Array|Function|Object|string): 这个函数会在每一次迭代调用。
  • [fromIndex=array.length-1] (number): The index to search from。
var users = [
  { 'user': 'barney',  'active': true },
  { 'user': 'fred',    'active': false },
  { 'user': 'pebbles', 'active': false }
];
 
_.findLastIndex(users, function(o) { return o.user == 'pebbles'; });
// => 2
 
// The `_.matches` iteratee shorthand.
_.findLastIndex(users, { 'user': 'barney', 'active': true });
// => 0
 
// The `_.matchesProperty` iteratee shorthand.
_.findLastIndex(users, ['active', false]);
// => 2
 
// The `_.property` iteratee shorthand.
_.findLastIndex(users, 'active');
// => 0

(14) _.flatten(array)

  • 减少一级array嵌套深度。
  • array (Array): 需要减少嵌套层级的数组。
_.flatten([1, [2, [3, [4]], 5]]);
// => [1, 2, [3, [4]], 5]

(15)_.flattenDeep(array)

  • 将array递归为一维数组。
_.flattenDeep([1, [2, [3, [4]], 5]]);
// => [1, 2, 3, 4, 5]

(16)_.flattenDepth(array, [depth=1])

  • 根据 depth 递归减少 array 的嵌套层级
var array = [1, [2, [3, [4]], 5]];
 
_.flattenDepth(array, 1);
// => [1, 2, [3, [4]], 5]
 
_.flattenDepth(array, 2);
// => [1, 2, 3, [4], 5]

(17)_.fromPairs(pairs)

  • 返回一个由键值对pairs构成的对象。
  • pairs (Array): 键值对pairs。
_.fromPairs([['fred', 30], ['barney', 40]]);
// => { 'fred': 30, 'barney': 40 }

(18)_.head(array)

  • 获取数组 array 的第一个元素。
_.head([1, 2, 3]);
// => 1
 
_.head([]);
// => undefined

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

  • 使用 SameValueZero 等值比较,返回首次 value 在数组array中被找到的 索引值, 如果 fromIndex 为负值,将从数组array尾端索引进行匹配。
  • array (Array): 需要查找的数组。
  • value (*): 需要查找的值。
  • [fromIndex=0] (number): 开始查询的位置。
_.indexOf([1, 2, 1, 2], 2);
// => 1
 
// Search from the `fromIndex`.
_.indexOf([1, 2, 1, 2], 2, 2);
// => 3

(20)_.initial(array)

  • 获取数组array中除了最后一个元素之外的所有元素(去除数组array中的最后一个元素)。
_.initial([1, 2, 3]);
// => [1, 2]

(21)_.intersection([arrays])

  • 创建唯一值的数组,这个数组包含所有给定数组都包含的元素,使用 SameValueZero进行相等性比较。(可理解为给定数组的交集)
  • [arrays] (…Array): 待检查的数组。
  • (Array): 返回一个包含所有传入数组交集元素的新数组。
_.intersection([2, 1], [4, 2], [1, 2]);
// => [2]

(22) _.intersectionBy([arrays], [iteratee= _.identity])

  • 这个方法类似 _.intersection,区别是它接受一个 iteratee 调用每一个arrays的每个值以产生一个值,通过产生的值进行了比较。结果值是从第一数组中选择。iteratee 会传入一个参数:(value)。
  • [arrays] (…Array): 待检查的数组。
  • [iteratee=_.identity] (Array|Function|Object|string): iteratee(迭代器)调用每个元素。
_.intersectionBy([2.1, 1.2], [4.3, 2.4], Math.floor);
// => [2.1]
 
// The `_.property` iteratee shorthand.
_.intersectionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }]

(23)_.intersectionWith([arrays], [comparator])

  • 这个方法类似 _.intersection,区别是它接受一个 comparator 调用比较arrays中的元素。结果值是从第一数组中选择。comparator 会传入两个参数:(arrVal, othVal)。
  • [arrays] (…Array): 待检查的数组。
  • [comparator] (Function): comparator(比较器)调用每个元素。
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
 
_.intersectionWith(objects, others, _.isEqual);
// => [{ 'x': 1, 'y': 2 }]

(24)_.join(array, [separator=’,’])

  • array 中的所有元素转换为由 separator 分隔的字符串。
  • array (Array): 要转换的数组。
  • [separator=’,’] (string): 分隔元素。
_.join(['a', 'b', 'c'], '~');
// => 'a~b~c'

(25)_.last(array)

  • 获取array中的最后一个元素。
_.last([1, 2, 3]);
// => 3

(26)_.lastIndexOf(array, value, [fromIndex=array.length-1])

  • 这个方法类似 _.indexOf ,区别是它是从右到左遍历array的元素。
  • array (Array): 要搜索的数组。
  • value (*): 要搜索的值。
  • [fromIndex=array.length-1] (number): 开始搜索的索引值。
_.lastIndexOf([1, 2, 1, 2], 2);
// => 3
 
// Search from the `fromIndex`.
_.lastIndexOf([1, 2, 1, 2], 2, 2);
// => 1

(27) _.nth(array, [n=0])

  • 获取array数组的第n个元素。如果n为负数,则返回从数组结尾开始的第n个元素。
  • array (Array): 要查询的数组。
  • [n=0] (number): 要返回元素的索引值。
var array = ['a', 'b', 'c', 'd'];
 
_.nth(array, 1);
// => 'b'
 
_.nth(array, -2);
// => 'c';

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

  • 移除数组array中所有和给定值相等的元素,这个方法会改变数组。
var array = [1, 2, 3, 1, 2, 3];
 
_.pull(array, 2, 3);
console.log(array);
// => [1, 1]

(29)_.pullAll(array, values)

  • 这个方法类似_.pull,区别是这个方法接收一个要移除值的数组。
  • array (Array): 要修改的数组。
  • values (Array): 要移除值的数组。
var array = [1, 2, 3, 1, 2, 3];
 
_.pullAll(array, [2, 3]);
console.log(array);
// => [1, 1]

(30) _.pullAllBy(array, values, [iteratee= _.identity])

  • 这个方法类似于_.pullAll ,区别是这个方法接受一个 iteratee(迭代函数) 调用 arrayvalues的每个值以产生一个值,通过产生的值进行了比较。iteratee 会传入一个参数: (value)。
  • array (Array): 要修改的数组。
  • values (Array): 要移除值的数组。
  • [iteratee=_.identity] (Array|Function|Object|string): iteratee(迭代器)调用每个元素。
var array = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }];
 
_.pullAllBy(array, [{ 'x': 1 }, { 'x': 3 }], 'x');
console.log(array);
// => [{ 'x': 2 }]

(31)_.pullAllWith(array, values, [comparator])

  • 这个方法类似于 _.pullAll,区别是这个方法接受 comparator 调用array中的元素和values比较。comparator 会传入两个参数:(arrVal, othVal)。
  • array (Array): 要修改的数组。
  • values (Array): 要移除值的数组。
  • [comparator] (Function): comparator(比较器)调用每个元素。
var array = [{ 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 }, { 'x': 5, 'y': 6 }];
 
_.pullAllWith(array, [{ 'x': 3, 'y': 4 }], _.isEqual);
console.log(array);
// => [{ 'x': 1, 'y': 2 }, { 'x': 5, 'y': 6 }]

(32)_.pullAt(array, [indexes])

  • 根据索引 indexes,移除array中对应的元素,并返回被移除元素的数组。
  • array (Array): 要修改的数组。
  • [indexes] (…(number|number[])): 要移除元素的索引。
var array = [5, 10, 15, 20];
var evens = _.pullAt(array, 1, 3);
 
console.log(array);
// => [5, 15]
 
console.log(evens);
// => [10, 20]

(33) _.remove(array, [predicate= _.identity])

  • 移除数组中predicate(断言)返回为真值的所有元素,并返回移除元素组成的数组。predicate(断言) 会传入3个参数: (value, index, array)。
  • array (Array): 要修改的数组。
  • [predicate=_.identity] (Array|Function|Object|string): 每次迭代调用的函数。
var array = [1, 2, 3, 4];
var evens = _.remove(array, function(n) {
  return n % 2 == 0;
});
 
console.log(array);
// => [1, 3]
 
console.log(evens);
// => [2, 4]

(34)_.reverse(array)

  • 反转array,使得第一个元素变为最后一个元素,第二个元素变为倒数第二个元素,依次类推。
  • 改变原数组。
var array = [1, 2, 3];
 
_.reverse(array);
// => [3, 2, 1]
 
console.log(array);
// => [3, 2, 1]

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

  • 裁剪数组array,从 start 位置开始到end结束,但不包括 end 本身的位置。
  • array (Array): 要裁剪数组。
  • [start=0] (number): 开始位置。
  • [end=array.length] (number): 结束位置。
_.slice([1,2,3,4,5,6,7],2,5);
//[3, 4, 5]

(36) _.sortedIndex(array, value)

  • 使用二进制的方式检索来决定 value值 应该插入到数组中 尽可能小的索引位置,以保证array的排序。
  • 返回 value值 应该在数组array中插入的索引位置 index。
_.sortedIndex([30, 50], 40);
// => 1

(37) _.sortedIndexBy(array, value, [iteratee= _.identity])

  • 这个方法类似 _.sortedIndex ,除了它接受一个 iteratee (迭代函数),调用每一个数组(array)元素,返回结果和value 值比较来计算排序。iteratee 会传入一个参数:(value)。
  • array (Array): 要检查的排序数组。
  • value (*): 要评估的值。
  • [iteratee=_.identity] (Array|Function|Object|string): 迭代函数,调用每个元素。
var objects = [{ 'x': 4 }, { 'x': 5 }];
 
_.sortedLastIndexBy(objects, { 'x': 4 }, function(o) { return o.x; });
// => 1
 
// The `_.property` iteratee shorthand.
_.sortedLastIndexBy(objects, { 'x': 4 }, 'x');
// => 1

(38)_.sortedLastIndexOf(array, value)

  • 这个方法类似 _.lastIndexOf,但它是在已经排序的数组array上执行二进制检索。
_.sortedLastIndexOf([4, 5, 5, 5, 6], 5);
// => 3

(39)_.sortedUniq(array)

  • 优化排序数组,删除重复的元素。
_.sortedUniq([1, 1, 2]);
// => [1, 2]

(40)_.sortedUniqBy(array, [iteratee])

  • array (Array): 要检查的数组。
  • [iteratee] (Function): 迭代函数,调用每个元素。
_.sortedUniqBy([1.1, 1.2, 2.3, 2.4], Math.floor);
// => [1.1, 2.3]

(41)_.tail(array)

  • 获取除了array数组第一个元素以外的全部元素。
_.tail([1, 2, 3]);
// => [2, 3]

(42)_.take(array, [n=1])

  • 创建一个数组切片,从array数组的起始元素开始提取n个元素。
  • array (Array): 要检索的数组。
  • [n=1] (number): 要提取的元素个数。默认是1。
_.take([1, 2, 3]);
// => [1]
 
_.take([1, 2, 3], 2);
// => [1, 2]
 
_.take([1, 2, 3], 5);
// => [1, 2, 3]
 
_.take([1, 2, 3], 0);
// => []

(43)_.takeRight(array, [n=1])

  • 创建一个数组切片,从array数组的最后一个元素开始提取n个元素。
  • array (Array): 要检索的数组。
  • [n=1] (number): 要提取的元素个数。
_.takeRight([1, 2, 3]);
// => [3]
 
_.takeRight([1, 2, 3], 2);
// => [2, 3]
 
_.takeRight([1, 2, 3], 5);
// => [1, 2, 3]
 
_.takeRight([1, 2, 3], 0);
// => []

(44) _.takeRightWhile(array, [predicate= _.identity])

  • 从array数组的最后一个元素开始提取元素,直到 predicate 返回假值。predicate 会传入三个参数: (value, index, array)。
  • array (Array): 要检索的数组。
  • [predicate=_.identity] (Array|Function|Object|string): 每次迭代调用的函数。
var users = [
  { 'user': 'barney',  'active': true },
  { 'user': 'fred',    'active': false },
  { 'user': 'pebbles', 'active': false }
];
 
_.takeRightWhile(users, function(o) { return !o.active; });
// => objects for ['fred', 'pebbles']
 
// The `_.matches` iteratee shorthand.
_.takeRightWhile(users, { 'user': 'pebbles', 'active': false });
// => objects for ['pebbles']
 
// The `_.matchesProperty` iteratee shorthand.
_.takeRightWhile(users, ['active', false]);
// => objects for ['fred', 'pebbles']
 
// The `_.property` iteratee shorthand.
_.takeRightWhile(users, 'active');
// => []

(45)_.takeWhile(array, [predicate= _.identity])

  • 从array数组的起始元素开始提取元素,,直到 predicate 返回假值。predicate 会传入三个参数: (value, index, array)。
  • array (Array): 需要处理的数组
  • [predicate=_.identity] (Array|Function|Object|string): 每次迭代调用的函数。
var users = [
  { 'user': 'barney',  'active': false },
  { 'user': 'fred',    'active': false},
  { 'user': 'pebbles', 'active': true }
];
 
_.takeWhile(users, function(o) { return !o.active; });
// => objects for ['barney', 'fred']
 
// The `_.matches` iteratee shorthand.
_.takeWhile(users, { 'user': 'barney', 'active': false });
// => objects for ['barney']
 
// The `_.matchesProperty` iteratee shorthand.
_.takeWhile(users, ['active', false]);
// => objects for ['barney', 'fred']
 
// The `_.property` iteratee shorthand.
_.takeWhile(users, 'active');
// => []

(46)_.union([arrays])

  • [arrays] (…Array): 要检查的数组。
  • 创建一个按顺序排列的唯一值的数组。返回数组的并集,按照顺序,并且数组的元素是唯一的。
_.union([2], [1, 2]);
// => [2, 1]

(47) _.unionBy([arrays], [iteratee= _.identity])

  • 这个方法类似 _.union ,除了它接受一个 iteratee (迭代函数),调用每一个数组(array)的每个元素以产生唯一性计算的标准。iteratee 会传入一个参数:(value)。
  • [arrays] (…Array): 要检查的数组。
  • [iteratee=_.identity] (Array|Function|Object|string): 迭代函数,调用每个元素。
_.unionBy([2.1], [1.2, 2.3], Math.floor);
// => [2.1, 1.2]
 
// The `_.property` iteratee shorthand.
_.unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]

(48)_.unionWith([arrays], [comparator])

  • 这个方法类似 _.union, 除了它接受一个 comparator 调用比较arrays数组的每一个元素。 comparator 调用时会传入2个参数: (arrVal, othVal)。
  • [arrays] (…Array): 要检查的数组。
  • [comparator] (Function): 比较函数,调用每个元素。
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
 
_.unionWith(objects, others, _.isEqual);
// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]

(49)_.uniq(array)

  • 创建一个去重后的array数组副本。使用了 SameValueZero 做等值比较。只有第一次出现的元素才会被保留。
_.uniq([2, 1, 2, 4, 5, 6, 2, 3, 4]);
// => [2, 1, 4, 5, 6, 3]

(50)_.uniqBy(array, [iteratee= _.identity])

  • 这个方法类似 _.uniq ,除了它接受一个 iteratee (迭代函数),调用每一个数组(array)的每个元素以产生唯一性计算的标准。iteratee 调用时会传入一个参数:(value)。
_.uniqBy([2.1, 1.2, 2.3], Math.floor);
// => [2.1, 1.2]
 
// The `_.property` iteratee shorthand.
_.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]

(51)_.uniqWith(array, [comparator])

  • 这个方法类似 _.uniq, 除了它接受一个 comparator 调用比较arrays数组的每一个元素。 comparator 调用时会传入2个参数: (arrVal, othVal)。
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
 
_.uniqWith(objects, _.isEqual);
// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]

(52)_.unzip(array)

  • 这个方法类似于_.zip,除了它接收分组元素的数组,并且创建一个数组,分组元素到打包前的结构。(返回数组的第一个元素包含所有的输入数组的第一元素,第一个元素包含了所有的输入数组的第二元素,依此类推。)
  • array (Array): 要处理的分组元素数组。
  • (Array): 返回重组元素的新数组。
var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]);
// => [['fred', 30, true], ['barney', 40, false]]
 
_.unzip(zipped);
// => [['fred', 'barney'], [30, 40], [true, false]]

(53)_.unzipWith(array, [iteratee= _.identity])

  • 此方法类似于_.unzip,除了它接受一个iteratee指定重组值应该如何被组合。iteratee 调用时会传入每个分组的值: (...group)。
var zipped = _.zip([1, 2], [10, 20], [100, 200]);
// => [[1, 10, 100], [2, 20, 200]]
 
_.unzipWith(zipped, _.add);
// => [3, 30, 300]

(54)_.without(array, [values])

  • 创建一个剔除所有给定值的新数组,剔除值的时候,使用SameValueZero做相等比较。
_.without([2, 1, 2, 3], 1, 2);
// => [3]

(55)_.xor([arrays])

  • 创建一个给定数组唯一值的数组,使用symmetric difference做等值比较。返回值的顺序取决于他们数组的出现顺序。
_.xor([2, 1], [2, 3]);
// => [1, 3]

(56)_.xorBy([arrays], [iteratee= _.identity])

  • 这个方法类似 _.xor ,除了它接受 iteratee(迭代器),这个迭代器 调用每一个 arrays(数组)的每一个值,以生成比较的新值。iteratee 调用一个参数: (value).
_.xorBy([2.1, 1.2], [2.3, 3.4], Math.floor);
// => [1.2, 3.4]
 
// The `_.property` iteratee shorthand.
_.xorBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 2 }]

(57)_.xorWith([arrays], [comparator])

  • 该方法是像 _.xor,除了它接受一个 comparator ,以调用比较数组的元素。 comparator 调用2个参数:(arrVal, othVal).
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
 
_.xorWith(objects, others, _.isEqual);
// => [{ 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]

(58)_.zip([arrays])

  • 创建一个分组元素的数组,数组的第一个元素包含所有给定数组的第一个元素,数组的第二个元素包含所有给定数组的第二个元素,以此类推。
_.zip(['fred', 'barney'], [30, 40], [true, false]);
// => [['fred', 30, true], ['barney', 40, false]]

(59)_.zipObject([props=[]], [values=[]])

  • 这个方法类似 _.fromPairs,除了它接受2个数组,第一个数组中的值作为属性标识符(属性名),第二个数组中的值作为相应的属性值。
_.zipObject(['a', 'b'], [1, 2]);
// => { 'a': 1, 'b': 2 }
_.zipObject(['a', 'b'], [1, 2, 5]);
// => { 'a': 1, 'b': 2 }

(60)_.zipObjectDeep([props=[]], [values=[]])

  • 这个方法类似 _.zipObject,除了它支持属性路径。
_.zipObjectDeep(['a.b[0].c', 'a.b[1].d'], [1, 2]);
// => { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }

(61)_.zipWith([arrays], [iteratee= _.identity])

  • 这个方法类似于_.zip,不同之处在于它接受一个 iteratee(迭代函数),来 指定分组的值应该如何被组合。 该iteratee调用每个组的元素: (…group)。
_.zipWith([1, 2], [10, 20], [100, 200], function(a, b, c) {
  return a + b + c;
});
// => [111, 222]

你可能感兴趣的:(Lodash)