Array

数组

Description:
  • 数组

Source:
Since:
  • 1.0.0

Methods

(static) chunk(array, sizeopt) → {Array}

Description:
  • 将数组拆分成多个 size 长度的区块,并将这些区块组成一个新数组。

    如果数组无法被分割成全部等长的区块,那么最后剩余的元素将组成一个区块。

Source:
Since:
  • 1.0.0
Example
const array = ['a', 'b', 'c', 'd'];

chunk(array, 2); // [['a', 'b'], ['c', 'd']]

chunk(array, 3); // [['a', 'b', 'c'], ['d']]
Parameters:
Name Type Attributes Default Description
array Array

要处理的数组。

size number <optional>
1

每个数组区块的长度。默认 1

Returns:

拆分区块的新数组。

Type
Array

(static) compact(array) → {Array}

Description:
  • 创建一个新数组,包含原数组中所有的非假值元素。

    例如 false null 0 "" undefined NaN 都被认为是假值。

Source:
Since:
  • 1.0.0
See:
Example
compact([0, 1, false, '', 2]); // [1, 2]
Parameters:
Name Type Description
array Array

待处理的数组。

Returns:

过滤掉假值的新数组。

Type
Array

(static) difference(array, values, iterateeopt, strictCheckopt) → {Array}

Description:
  • 创建一个 array 排除 values 值的新数组。如果传入迭代函数,会调用数组的每个元素以产生唯一性计算的标准。

    iteratee 调用时会传入一个参数 value

    默认使用了 SameValueZero 做等值比较。如果 strictCheck=true 将使用 SameValue 做等值比较。

Source:
Since:
  • 1.0.0
Example
difference([3, 1, 2], [4, 2]); // [3, 1]

difference([3.1, 2.2, 1.3], [4.4, 2.5], Math.floor); // [3.1, 1.3]

difference([{x: 2}, {x: 1}], [{x: 1}], item=>item.x); // [{x: 2}]

// 如果迭代元素为对象,迭代函数可以直接写入对象属性。
difference([{x: 2}, {x: 1}], [{x: 1}], 'x'); // [{x: 2}]

difference([-0, 0], [0]); // []

difference([-0, 0], [0], undefined, true); // [-0]
Parameters:
Name Type Attributes Default Description
array Array

要检查的数组。

values Array

排除的值。

iteratee function | string | number | Symbol <optional>
identity

迭代函数,调用每个元素。默认 identity

strictCheck boolean <optional>
false

严格比较,区分 0 -0。默认 false

Returns:

过滤值后的新数组。

Type
Array

(static) fromPairs(array) → {Object}

Description:
Source:
Since:
  • 1.0.0
Example
fromPairs([['foo', 'bar'], ['baz', 42]]); // {foo: 'bar', baz: 42}
Parameters:
Name Type Description
array Array

键值对数组。

Returns:

新对象。

Type
Object

(static) intersection(array, other, iterateeopt, strictCheckopt) → {Array}

Description:
  • 创建唯一值的数组,该数组包含两个数组参数都包含的元素(交集)。如果传入迭代函数,会调用数组的每个元素以产生唯一性计算的标准。

    iteratee 调用时会传入一个参数 value

    默认使用了 SameValueZero 做等值比较。如果 strictCheck=true 将使用 SameValue 做等值比较。

Source:
Since:
  • 1.0.0
Example
intersection([2, 1, 1], [4, 2]); // [2]

intersection([2.1, 1.2], [4.3, 2.4], Math.floor); // [2.1]

intersection([{x: 1}, {x: 1}, {x: 2}, {x: 2}], [{x: 1}], item=>item.x); // [{x: 1}]

// 迭代函数可以直接写入属性。
intersection([{x: 1}, {x: 1}, {x: 2}, {x: 2}], [{x: 1}], 'x'); // [{x: 1}]

intersection([-0, 0], [0]); // [-0]

intersection([-0, 0], [0], undefined, true); // [0]
Parameters:
Name Type Attributes Default Description
array Array

要检查的数组。

other Array

另一个要检查的数组。

iteratee function | string | number | Symbol <optional>
identity

迭代函数,调用每个元素。默认 identity

strictCheck boolean <optional>
false

严格比较,区分 0 -0。默认 false

Returns:

包含所有传入数组交集元素的新数组。

Type
Array

(static) move(array, from, to) → {*}

Description:
  • 将数组 from 位置的元素移至 to 位置,返回处理后的原数组。

Source:
Since:
  • 1.10.0
See:
Example
const arr = ['a', 'b', 'c', 'd'];

move(arr, 0, 1); // ['b', 'a', 'c', 'd']

// 此时 arr 已经变为 ['b', 'a', 'c', 'd']
move(arr, -2, 0); // ['c', 'b', 'a', 'd']
Parameters:
Name Type Description
array Array

要处理的数组。

from number

要移动的元素索引。

to number

要移动目标位置的元素索引。

Returns:

处理后的原数组。

Type
*

(static) nth(array, nopt) → {*}

Description:
  • 获取类数组的第 n 个元素。如果 n 为负数,则返回从数组结尾开始的第 n 个元素。

    Array.prototype.at 方法。

Source:
Since:
  • 1.0.0
Example
const arr = ['a', 'b', 'c', 'd'];

nth(arr, 1); // 'b'

nth(arr, -2); // 'c'

nth('abcd', 1); // 'b'
Parameters:
Name Type Attributes Default Description
array ArrayLike

要查询的类数组。

n number <optional>
0

要返回元素的索引值。默认 0

Returns:

类数组的第 n 个元素。

Type
*

(static) shuffle(array) → {Array}

Description:
Source:
Since:
  • 1.0.0
Example
shuffle([1, 2, 3, 4]); // [2, 4, 3, 1]

shuffle([1, 2, 3, 4]); // [3, 2, 1, 4]
Parameters:
Name Type Description
array Array

要打乱的数组。

Returns:

打乱的数组。

Type
Array

(static) union(array, otheropt, iterateeopt, strictCheckopt) → {Array}

Description:
  • 创建一个按顺序排列的唯一值的数组(并集)。如果传入迭代函数,会调用数组的每个元素以产生唯一性计算的标准。与 uniq 相似。

    iteratee 调用时会传入一个参数 value

    默认使用了 SameValueZero 做等值比较。如果 strictCheck=true 将使用 SameValue 做等值比较。

Source:
Since:
  • 1.0.0
Example
union([2], [1, 2]); // [2, 1]

union([2.1], [1.2, 2.3], Math.floor); // [2.1, 1.2]

union([{x: 1}, {x: 1}, {x: 2}, {x: 2}], [{x: 1}], item=>item.x); // [{x: 1}, {x: 2}]

// 迭代函数可以直接写入属性。
union([{x: 1}, {x: 1}, {x: 2}, {x: 2}], [{x: 1}], 'x'); // [{x: 1}, {x: 2}]

union([-0, 0], [-0]); // [-0]

union([-0, 0], [-0], undefined, true); // [-0, 0]
Parameters:
Name Type Attributes Default Description
array Array

要检查的数组。

other Array <optional>
[]

另一个要检查的数组。

iteratee function | string | number | Symbol <optional>

迭代函数,调用每个元素。

strictCheck boolean <optional>
false

严格比较,区分 0 -0。默认 false

Returns:

新的联合数组。

Type
Array

(static) uniq(array, iterateeopt, strictCheckopt) → {Array}

Description:
  • 创建一个去重后的数组副本。只有第一次出现的元素才会被保留。如果传入迭代函数,会调用数组的每个元素以产生唯一性计算的标准。

    iteratee 调用时会传入一个参数 value

    默认使用了 SameValueZero 做等值比较。如果 strictCheck=true 将使用 SameValue 做等值比较。

Source:
Since:
  • 1.0.0
Example
uniq([2, 1, 2]); // [2, 1]

uniq(['a', NaN, 2, 1, NaN, 'a', 1]); // ['a', NaN, 2, 1]

uniq([{x: 1}, {x: 2}, {x: 1}], item=>item.x); // [{x: 1}, {x: 2}]

// 迭代函数可以直接写入属性。
uniq([{x: 1}, {x: 2}, {x: 1}], 'x'); // [{x: 1}, {x: 2}]

uniq([-0, 0]); // [-0]

uniq([-0, 0], undefined, true); // [-0, 0]
Parameters:
Name Type Attributes Default Description
array Array

要检查的数组。

iteratee function | string | number | Symbol <optional>

迭代函数,调用每个元素。

strictCheck boolean <optional>
false

严格比较,区分 0 -0。默认 false

Returns:

去重后的新数组。

Type
Array

(static) unzip(array) → {Array}

Description:
  • zip 类似,除了它接受分组元素的数组,并且创建一个数组,分组元素到打包前的结构。(返回数组的第一个元素包含所有的输入数组的第一元素,第二个元素包含了所有的输入数组的第二元素,依此类推。)。

Source:
Since:
  • 1.0.0
Example
unzip([['barney', 'fred'], [36, 40]]); // [['barney', 36], ['fred', 40]]

unzip([['barney', 36], ['fred', 40]]); // [['barney', 'fred'], [36, 40]]
Parameters:
Name Type Description
array Array

要处理的分组元素数组。

Returns:

重组元素的新数组。

Type
Array

(static) xor(array, otheropt, iterateeopt, strictCheckopt) → {Array}

Description:
  • 创建一个唯一值的数组(并集-交集),该数组包含两个数组参数中不相同的元素。如果传入迭代函数,会调用数组的每个元素以产生唯一性计算的标准。

    iteratee 调用时会传入一个参数 value

    默认使用了 SameValueZero 做等值比较。如果 strictCheck=true 将使用 SameValue 做等值比较。

Source:
Since:
  • 1.0.0
Example
xor([2, 1, 1], [4, 2]); // [1, 4]

xor([2.1, 1.2], [4.3, 2.4], Math.floor); // [1.2, 4.3]

xor([{x: 1}, {x: 1}, {x: 2}, {x: 2}], [{x: 1}], item=>item.x); // [{x: 2}]

// 迭代函数可以直接写入属性。
xor([{x: 1}, {x: 1}, {x: 2}, {x: 2}], [{x: 1}], 'x'); // [{x: 2}]

xor([-0, 0],[0]); // []

xor([-0, 0],[0], undefined, true); // [-0]
Parameters:
Name Type Attributes Default Description
array Array

要检查的数组。

other Array <optional>
[]

另一个要检查的数组。

iteratee function | string | number | Symbol <optional>
identity

迭代函数,调用每个元素。默认 identity

strictCheck boolean <optional>
false

严格比较,区分 0 -0。默认 false

Returns:

过滤值后的新数组。

Type
Array

(static) zip(…array) → {Array}

Description:
  • 创建一个数组,数组的第一个元素包含所有给定数组的第一元素,第二个元素包含了所有给定数组的第二元素,依此类推。

Source:
Since:
  • 1.0.0
Example
zip(['barney', 'fred'], [36, 40]); // [['barney', 36], ['fred', 40]]
Parameters:
Name Type Attributes Description
array Array <repeatable>

要处理的分组元素数组。

Returns:

重组元素的新数组。

Type
Array