Module: array

数组/类数组相关函数

Functions

append

append<T>(array, ...values): T

向数组末尾追加一个或多个元素并返回

该函数会修改原数组

Example

//[1, 2, 3, 4]let ary = [1,2];_.append(ary,3,4);console.log(ary);//[1, 2, Array(2), 5]ary = [1,2];_.append(ary,[3,4],5);console.log(ary);//[1, 2, 3, 4]ary = [1,2];_.append(ary,...[3,4]);console.log(ary);

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组对象。如果非数组类型会自动转为数组
...valuesany1-n个需要插入列表的值

Returns

T

插入值后的数组对象


chunk

chunk<T>(array, size?): T[]

把指定数组拆分成多个长度为size的子数组,并返回子数组组成的二维数组

Example

//[[1,2],[3,4]]console.log(_.chunk([1,2,3,4],2))//[[1,2,3],[4]]console.log(_.chunk([1,2,3,4],3))

Since

0.23.0

Type parameters

Name
T

Parameters

NameTypeDefault valueDescription
arrayTundefined数组对象。如果非数组类型会转成数组
size?number1子数组长度

Returns

T[]

拆分后的新数组


compact

compact<T>(array): T

对集合内的假值进行剔除,并返回剔除后的新数组。假值包括 null/undefined/NaN/0/''/false

Example

//[1,2,4,'a','1']console.log(_.compact([0,1,false,2,4,undefined,'a','1','',null]))

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组

Returns

T

转换后的新数组对象


concat

concat(...arrays): any

合并数组或值并返回新数组,元素可以重复。基于 Array.prototype.concat 实现

Example

//[a/b/a]console.log(_.concat([{name:'a'},{name:'b'}],[{name:'a'}]))//[1, 2, 3, 1, 2]console.log(_.concat([1,2,3],[1,2]))//[1, 2, 3, 1, 2, null, 0]console.log(_.concat([1,2,3],[1,2],null,0))//[1, 2, 3, 1, 2, doms..., 0, null]console.log(_.concat([1,2,3],[1,2],document.body.children,0,null))

Parameters

NameTypeDescription
...arraysany1-n个数组对象

Returns

any

如果参数为空,返回空数组


except

except(...params): any

对所有集合做差集并返回差集元素组成的新数组

Example

//[1]console.log(_.except([1,2,3],[2,3]))//[1,4]console.log(_.except([1,2,3],[2,3],[3,2,1,4]))//[{name: "b"}]console.log(_.except([{name:'a'},{name:'b'}],[{name:'a'}],v=>v.name))//[2, 3, "2", "3"] '2'和2不相等console.log(_.except([1,2,3],[1,'2',3],[2,'3',1]))

Parameters

NameType
...paramsany

Returns

any

差集元素组成的新数组


fill

fill<T>(array, value, start?, end?): T

使用固定值填充arrayLike中从起始索引到终止索引内的全部元素

Example

//[6, 6, 6]console.log(_.fill(new Array(3), 6))//[1, 'x', 'x', 'x', 5]console.log(_.fill([1, 2, 3, 4, 5], 'x', 1, 4))

Type parameters

Name
T

Parameters

NameTypeDefault valueDescription
arrayTundefined数组
valueanyundefined填充值
start?number0起始索引,包含
end?numberundefined终止索引,不包含

Returns

T

填充后的新数组


findIndex

findIndex<T>(array, predicate, fromIndex?): number

对集合内的所有元素进行断言并返回第一个匹配的元素索引

Example

//3 查询数组的索引console.log(_.findIndex(['a','b','c',1,3,6],_.isNumber))//0console.log(_.findIndex([{a:1},{a:2},{a:3}],'a'))//2console.log(_.findIndex([{a:1},{a:2},{a:3}],{a:3}))

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组
predicateNonFuncItee | (value: T, index: string | number, array: T) => boolean(value,index,array);断言
当断言是函数时回调参数见定义
其他类型请参考 iteratee
fromIndex?number从0开始的起始索引,设置该参数可以减少实际遍历次数。默认0

Returns

number

第一个匹配断言的元素索引或-1


findLastIndex

findLastIndex<T>(array, predicate, fromIndex?): number

对集合内的所有元素进行断言并返回最后一个匹配的元素索引

Example

//5 查询数组的索引console.log(_.findLastIndex(['a','b','c',1,3,6],_.isNumber))//2console.log(_.findLastIndex([{a:1},{a:2},{a:3}],'a'))

Since

0.19.0

Type parameters

Name
T

Parameters

NameTypeDescription
arrayTarrayLike对象及set对象
predicateNonFuncItee | (value: T, index: string | number, array: T) => boolean(value,index,array);断言
当断言是函数时回调参数见定义
其他类型请参考 iteratee
fromIndex?number-

Returns

number

最后一个匹配断言的元素索引或-1


first

first<T>(array): T

获取数组中的第一个元素

Example

//1console.log(_.first([1,2,3]))//"1"console.log(_.first(new Set(['1',1])))

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组

Returns

T

数组中第一个元素


flat

flat<T>(array, depth?): T

按照指定的嵌套深度递归遍历数组,并将所有元素与子数组中的元素合并为一个新数组返回

Example

//[1,2,3,4,5]console.log(_.flat([1,[2,3],[4,5]]))//[1,2,3,4,5,[6,7]]console.log(_.flat([1,[2,3],[4,5,[6,7]]]))//[1,2,3,[4]]console.log(_.flat([1,[2,[3,[4]]]],2))//[1,2,1,3,4]console.log(_.flat(new Set([1,1,[2,[1,[3,4]]]]),Infinity))

Type parameters

Name
T

Parameters

NameTypeDefault valueDescription
arrayanyundefined数组
depth?number1嵌套深度

Returns

T

扁平化后的新数组


flatDeep

flatDeep<T>(array): T

无限深度遍历数组,并将所有元素与子数组中的元素合并为一个新数组返回

Example

//[1,2,1,3,4]console.log(_.flatDeep(new Set([1,1,[2,[1,[3,4]]]])))//[1,2,3,4]console.log(_.flatDeep([1,[2,[3,[4]]]]))

Type parameters

Name
T

Parameters

NameTypeDescription
arrayany数组

Returns

T

扁平化后的新数组


head<T>(array): T

first()的别名函数

Function

Type parameters

Name
T

Parameters

NameTypeDescription
arrayTarrayLike对象及set对象

Returns

T

数组中第一个元素


initial

initial<T>(array): T

返回除最后一个元素外的所有元素组成的新数组

Example

//[1, 2]console.log(_.initial([1, 2, 3]))

Since

0.19.0

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组

Returns

T

新数组


insert

insert<T>(array, index, ...values): T

向数组中指定位置插入一个或多个元素并返回

该函数会修改原数组

Example

//[1, 2, Array(1), 'a', 3, 4]let ary = [1,2,3,4];_.insert(ary,2,[1],'a');console.log(ary);//[1, 2, 3, 4]ary = [3,4];_.insert(ary,0,1,2);console.log(ary);//func.jsconsole.log(_.insert('funcjs',4,'.').join(''));

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组对象。如果非数组类型会自动转为数组
indexnumber插入位置索引,0 - 列表长度
...valuesany1-n个需要插入列表的值

Returns

T

插入值后的数组对象


intersect

intersect(...params): any

对所有集合做交集并返回交集元素组成的新数组

关于算法性能可以查看文章《如何实现高性能集合操作(intersect)》

Example

//[2]console.log(_.intersect([1,2,3],[2,3],[1,2]))//[3]console.log(_.intersect([1,1,2,2,3],[1,2,3,4,4,4],[3,3,3,3,3,3]))//[{name: "a"}] 最后一个参数是函数时作为标识函数console.log(_.intersect([{name:'a'},{name:'b'}],[{name:'a'}],v=>v.name))//[]console.log(_.intersect())//[3] 第三个参数被忽略,然后求交集console.log(_.intersect([1,2,3],[3],undefined))//[1] "2"和2不相同,3和"3"不相同console.log(_.intersect([1,2,3],[1,'2',3],[2,'3',1]))

Parameters

NameType
...paramsany

Returns

any

交集元素组成的新数组


join

join(array, separator?): string

把arrayLike中所有元素连接成字符串并返回。对于基本类型元素会直接转为字符值,对象类型会调用toString()方法

Example

//'1/2/3/4'console.log(_.join([1, 2, 3, 4], '/'))//'1,2,3,4'console.log(_.join([1, 2, 3, 4]))

Parameters

NameTypeDescription
arrayany数组
separator?string分隔符

Returns

string

拼接字符串


last

last<T>(array): T

获取数组中的最后一个元素

Example

//3console.log(_.last([1,2,3]))

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组

Returns

T

数组中最后一个元素


pop

pop<T>(array, index?): T | null

删除数组末尾或指定索引的一个元素并返回被删除的元素。

该函数会修改原数组

Example

//3, [1, 2]let ary = [1,2,3];console.log(_.pop(ary),ary)//{a: 1}, [{"a":2},{"a":3}]ary = [{a:1},{a:2},{a:3}];console.log(_.pop(ary,0),ary)

Type parameters

Name
T

Parameters

NameTypeDescription
arrayunknown数组对象。如果非数组类型会直接返回null
index?number要删除元素的索引。默认删除最后一个元素

Returns

T | null

被删除的值或null


pull

pull<T>(array, ...values): T

与without相同,但会修改原数组

该函数会修改原数组

Example

//[1, 1] truelet ary = [1,2,3,4,3,2,1];let newAry = _.pull(ary,2,3,4)console.log(newAry,ary === newAry)

Since

0.19.0

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组对象
...valuesT需要删除的值

Returns

T

新数组


range

range(end): number

生成一个由(包含)start到(不包含)end的数字元素组成的数组。 根据参数个数不同,分为三种签名

_.range(end); _.range(start,end); _.range(start,end,step);

Example

//[0, 1, 2, 3, 4]console.log(_.range(5))//[0, -1, -2, -3, -4]console.log(_.range(-5))//[0, -0.5, -1, -1.5, -2, -2.5, -3, -3.5, -4, -4.5]console.log(_.range(0,-5,0.5))//[-5, -4, -3, -2, -1, 0]console.log(_.range(-5,1))

Parameters

NameTypeDescription
endnumber结束数

Returns

number

数字数组

range(start, end): number

Parameters

NameType
startnumber
endnumber

Returns

number

range(start, end, step): number

Parameters

NameType
startnumber
endnumber
stepnumber

Returns

number


remove

remove<T>(array, predicate): T

删除数组中断言结果为true的元素并返回被删除的元素

该函数会修改原数组

Example

//[1, 3] [2, 4]let ary = [1,2,3,4];console.log(_.remove(ary,x=>x%2),ary)//[2] [1,3]ary = [{a:1},{a:2},{a:3}];console.log(_.remove(ary,v=>v.a===2),ary)//[3] [1,2]ary = [{a:1},{a:2},{a:3}];console.log(_.remove(ary,{a:3}),ary)

Since

0.19.0

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组对象,如果参数非数组直接返回
predicateNonFuncItee | (value: T, index: string | number, array: T) => boolean(value,index,array);断言
当断言是函数时回调参数见定义
其他类型请参考 iteratee

Returns

T

被删除的元素数组或空数组


reverse

reverse<T>(array): T

对数组元素位置进行颠倒,返回改变后的数组。

Example

//[3, 2, 1]console.log(_.reverse([1, 2, 3]))

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组

Returns

T

颠倒后的新数组


slice

slice<T>(array, begin?, end?): T

对数组进行切片,并返回切片后的新数组,原数组不变。新数组内容是对原数组内容的浅拷贝

Example

//[2,3,4]console.log(_.slice([1,2,3,4,5],1,4))//[2,3,4,5]console.log(_.slice([1,2,3,4,5],1))

Type parameters

Name
T

Parameters

NameTypeDescription
arrayArrayLike<unknown> | T数组
begin?number切片起始下标,包含下标位置元素
end?number切片结束下标,不包含下标位置元素

Returns

T

切片元素组成的新数组


sortedIndex

sortedIndex<T>(array, value): number

使用二分法确定在array保持排序不变的情况下,value可以插入array的最小索引

Example

//1console.log(_.sortedIndex([1,2,3],1.5))//1console.log(_.sortedIndex(['a', 'c'], 'b'))//0console.log(_.sortedIndex([{a:1},{a:2},{a:3}], {a:2.5}))

Since

1.0.0

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT对象属性标识符数组
valueany需要插入数组的值

Returns

number

array索引


sortedIndexBy

sortedIndexBy<T>(array, value, itee?): number

sortedIndex,但支持自定义回调用来获取对比值

Example

//2console.log(_.sortedIndexBy([{a:1},{a:2},{a:3}], {a:2.5},'a'))

Since

1.0.0

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT-
valueany需要插入数组的值
itee?NonFuncItee | (value: any) => any-

Returns

number

array索引


tail

tail<T>(array): T

返回除第一个元素外的所有元素组成的新数组

Example

//[2, 3]console.log(_.tail([1, 2, 3]))

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组

Returns

T

新数组


take

take<T>(array, length?): T

从起始位置获取指定数量的元素并放入新数组后返回

Example

//[1, 2, 3]console.log(_.take([1, 2, 3, 4, 5],3))//[1, 2, 3, 4, 5]console.log(_.take([1, 2, 3, 4, 5]))

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组
length?number获取元素数量,默认数组长度

Returns

T

新数组


takeRight

takeRight<T>(array, length?): T

从数组末尾位置获取指定数量的元素放入新数组并返回

Example

//[3, 4, 5]console.log(_.takeRight([1, 2, 3, 4, 5],3))//[1, 2, 3, 4, 5]console.log(_.takeRight([1, 2, 3, 4, 5]))

Since

1.2.0

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组
length?number

Returns

T

新数组


union

union(...params): any

对所有集合做并集并返回并集元素组成的新数组。并集类似concat()但不允许重复值

Example

//[1, 2, 3]console.log(_.union([1,2,3],[2,3]))//[1, 2, 3, "1", "2"]console.log(_.union([1,2,3],['1','2']))//[{name: "a"},{name: "b"}]console.log(_.union([{name:'a'},{name:'b'}],[{name:'a'}],v=>v.name))//[a/b/a] 没有标识函数无法去重console.log(_.union([{name:'a'},{name:'b'}],[{name:'a'}]))//[1, 2, 3, "3"] "3"和3不相等console.log(_.union([1,2,3],[1,3],[2,'3',1]))

Parameters

NameType
...paramsany

Returns

any

并集元素组成的新数组


uniq

uniq<T>(array): T

对数组内的值进行去重

Example

// [1,2,4,"a","1",null]console.log(_.unique([1,2,2,4,4,'a','1','a',null,null]))

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组

Returns

T

转换后的新数组对象


uniqBy

uniqBy<T>(array, itee): T

uniq,但支持自定义筛选函数

Example

// [{"a":1},{"a":"1"},{"a":2},{"a":"2"}]console.log(_.uniqBy([{a:1},{a:1},{a:'1'},{a:2},{a:'2'},{a:2}],'a'))// [{"a":1},{"a":2}]console.log(_.uniqBy([{a:1},{a:1},{a:'1'},{a:2},{a:'2'},{a:2}],v=>v.a>>0))

Since

1.1.0

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组
iteeNonFuncItee | (value: T, index: UnknownMapKey) => boolean-

Returns

T

去重后的新数组对象


unzip

unzip(array): any[]

zip的反操作

Example

//[[1,2,undefined],['a','b','c']]console.log(_.unzip([[1, 'a'],[2, 'b'],[undefined, 'c']]))//[['a', 'b', 'c'], [1, 2, undefined],['1', undefined,undefined]]console.log(_.unzip([['a', 1, '1'], ['b', 2],['c']]))

Since

0.23.0

Parameters

NameTypeDescription
arrayany包含若干分组的数组

Returns

any[]

重新分组后的新数组


without

without<T>(array, ...values): T

返回删除所有values后的新数组。使用eq函数进行等值判断

Example

//[1, 1]console.log(_.without([1,2,3,4,3,2,1],2,3,4))

Since

0.19.0

Type parameters

Name
T

Parameters

NameTypeDescription
arrayT数组对象
...valuesT需要删除的值

Returns

T

新数组


zip

zip(...arrays): any[]

创建一个由指定数组arrays内元素重新分组后组成的二维数组, 第一个子数组由每个数组内的第一个元素组成,第二个子数组由每个数组内的第二个元素组成,以此类推。 子数组的数量由参数中数组内元素最多的数组决定。

Example

//[[1, 'a'],[2, 'b'],[undefined, 'c']]console.log(_.zip([1,2],['a','b','c']))//[['a', 1, '1'], ['b', 2, undefined],['c', undefined,undefined]]console.log(_.zip(['a','b','c'],[1,2],['1']))

Since

0.23.0

Parameters

NameTypeDescription
...arraysany[]1-n个数组

Returns

any[]

重新分组后的新数组


zipObject

zipObject(keys, values): Record<UnknownMapKey, any>

创建一个对象,属性名称与属性值分别来自两个数组

Example

//{a: 1, b: 2}console.log(_.zipObject(['a','b'],[1,2,3]))

Since

0.23.0

Parameters

NameTypeDescription
keys(string | number | symbol)对象属性标识符数组
valuesany对象值数组

Returns

Record<UnknownMapKey, any>

组合后的对象


zipWith

zipWith(...params): any[]

zip相同,但支持自定义组合逻辑

Example

//[[1, 3, 5], [2, 4, 6]]console.log(_.zipWith([1,2],[3,4],[5,6]))//[9, 12]console.log(_.zipWith([1,2],[3,4],[5,6],_.sum))//[3, 4]console.log(_.zipWith([1,2],[3,4],[5,6],group=>_.avg(group)))

Since

1.0.0

Parameters

NameType
...paramsany

Returns

any[]

重新分组后的新数组