Methods
allEqual(arr) → {Boolean}
- Source:
检查数组各项相等
Example
allEqual([1, 1, 1]); // true
allEqual([1, 2, 3, 4, 5, 6]); // false
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array |
Returns:
- Type
- Boolean
approximatelyEqual(v1, v2, epsilonopt) → {Boolean}
- Source:
判断两个数字约等于
Example
approximatelyEqual(Math.PI / 2.0, 1.5708, 0.001); // true
approximatelyEqual(Math.PI / 2.0, 2.5708, 0.002); // false
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
v1 |
Number | |||
v2 |
Number | |||
epsilon |
number |
<optional> |
0.001
|
范围 |
Returns:
- Type
- Boolean
arrScrambling(arr) → {Array}
- Source:
数组乱序
Example
arrScrambling([1,5,9]) // [5,1,9]
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array |
Returns:
- Type
- Array
bifurcate(arr, filter) → {*}
- Source:
拆分断言后的数组
Example
bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]) // [ ['beep', 'boop', 'bar'], ['foo'] ]
Parameters:
Name | Type | Description |
---|---|---|
arr |
* | |
filter |
* |
Returns:
- Type
- *
booleanAll(arr, fnopt) → {Boolean}
- Source:
判断arr数组中,满足fn,如果全部都满足即为true;如果有一个不满足条件即为false
Example
booleanAll([4, 2, 3], x => x > 1); // true
booleanAll([4, 2, 3], x => x > 2); // false
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
arr |
Array | |||
fn |
function |
<optional> |
Boolean
|
Returns:
- Type
- Boolean
castArray(val) → {Array}
- Source:
其它类型转数组
Example
castArray(1); // [1]
castArray([1]); // [1]
castArray("消息No One Time跟我说"); // ["消息No One Time跟我说"]
Parameters:
Name | Type | Description |
---|---|---|
val |
* | Array/Number/Boolean/String |
Returns:
- Type
- Array
compact(arr) → {Array}
- Source:
去除数组中的无效/无用值
Example
compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]) // [1, 2, 3, "a", "s", 34]
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array | 目标数组 |
Returns:
- Type
- Array
countOccurrences(arr, value) → {Number}
- Source:
数组中某元素出现的次数 ([1,2,2,3],2)
Example
countOccurrences([1,2,2,3],2) // 2
countOccurrences([1, 1, 2, 1, '1', '1', 2, 3], "1"); // 2
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array | |
value |
Number/String |
Returns:
- Type
- Number
deepCopy(obj) → {Object/Array}
- Source:
简单的深拷贝
Example
const person={
name:'xiaoming',
child:{
name:'Jack',
eat: function(){
console.log('阿巴');
}
}
}
let personCopy = deepCopy(person)
personCopy.child.eat = []
console.log(person.child.eat); // fn()
console.log(personCopy.child.eat); // []
// 确实两个对象值引用不相同了
Parameters:
Name | Type | Description |
---|---|---|
obj |
Object/Array | 目标数组 |
Returns:
目标数组Copy
- Type
- Object/Array
deepFlatten(arr) → {Array}
- Source:
递归扁平化数组
Example
deepFlatten([1, [2], [[3], 4], 5]) // [1, 2, 3, 4, 5]
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array | 目标数组 |
Returns:
一维数组
- Type
- Array
difference(arr1, arr2) → {Array}
- Source:
寻找差异(查找两个数组之间的差异,并返回第一个数组独有的)
Example
difference([1, 2, 3], [1, 5, 4]) // [2, 3]
Parameters:
Name | Type | Description |
---|---|---|
arr1 |
Array | |
arr2 |
Array |
Returns:
- Type
- Array
differenceBy(arr1, arr2, fn) → {Array}
- Source:
先执行再寻找差异(将给定函数应用于两个列表的每个元素之后,此方法返回两个数组之间的差异)
Example
differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor) // [1.2]
differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], v => v.x) // [ { x: 2 } ]
Parameters:
Name | Type | Description |
---|---|---|
arr1 |
Array | |
arr2 |
Array | |
fn |
function | 给定函数 |
Returns:
- Type
- Array
dropWhile(arr, func) → {Array}
- Source:
删除不符合条件的值
Example
dropWhile([1, 2,3, 4], n => n >= 3) // [3, 4]
只要第一个返回true就不判断后面的了
dropWhile([1, 3,2,1, 4], n => n >= 3) // [3, 2, 1, 4]
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array | |
func |
function |
Returns:
- Type
- Array
end()
- Source:
打印结束点
equals(a, b) → {Boolean}
- Source:
全等判断
Example
equals({ a: [2, { e: 3 }], b: [4], c: 'foo' }, { a: [2, { e: 3 }], b: [4], c: 'foo' }); // true
Parameters:
Name | Type | Description |
---|---|---|
a |
* | 目标变量a |
b |
* | 目标变量b |
Returns:
- Type
- Boolean
flatten(arr, depthopt) → {Array}
- Source:
指定深度扁平化数组
Example
flatten([1, [2], 3, 4]); // [1, 2, 3, 4]
flatten([1, [2, [3, [4, 5], 6], 7], 8], 2); // [1, 2, 3, [4, 5], 6, 7, 8]
flatten([1, [2, [3, [4, 5], 6], 7], 8], 3); // [1, 2, 3, 4, 5, 6, 7, 8]
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
arr |
Array | 目标数组 | ||
depth |
number |
<optional> |
1
|
指定深度 |
Returns:
- Type
- Array
inArray(item, data) → {Number}
- Source:
查询数组中是否存在某个元素并返回元素第一次出现的下标
Example
inArray(2,[1,2,3,4]) // 1
Parameters:
Name | Type | Description |
---|---|---|
item |
* | 要查询的元素 |
data |
Array |
Returns:
元素第一次出现的下标
- Type
- Number
indexOfAll(arr, val) → {Array}
- Source:
返回数组中某值的所有索引(如果此值中未包含该值,则返回一个空数组)
Example
indexOfAll([1, 2, 3, 1, 2, 3], 1) // [0,3]
indexOfAll([1, 2, 3], 4) // []
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array | 目标数组 |
val |
String | 某值 |
Returns:
- Type
- Array
intersection(arr1, arr2) → {Array}
- Source:
数组交集(方法二)
Example
intersection([1,2,3],[5,2]) // [2]
Parameters:
Name | Type | Description |
---|---|---|
arr1 |
Array | |
arr2 |
Array |
Returns:
- Type
- Array
intersectionBy(arr1, arr2, fn) → {Array}
- Source:
两数组都符合条件的交集
Example
intersectionBy([2.1,2.5, 1.2], [2.3, 3.4], Math.floor) // [2.1, 2.5]
Parameters:
Name | Type | Description |
---|---|---|
arr1 |
Array | |
arr2 |
Array | |
fn |
function |
Returns:
- Type
- Array
listChunk(list, sizeopt, cacheListopt) → {*}
- Source:
分割指定长度的元素数组
Example
listChunk([1, 2, 3, 4, 5, 6, 7, 8, 9]) // [[1], [2], [3], [4], [5], [6], [7], [8], [9]]
listChunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 3) // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
listChunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 0) // []
listChunk([1, 2, 3, 4, 5, 6, 7, 8, 9], -1) // []
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
list |
Array | 传进来的数组 | ||
size |
number |
<optional> |
1
|
要分成几个为一组的数据 |
cacheList |
Array |
<optional> |
[]
|
返回出去的结果 |
Returns:
- Type
- *
minN(arr, nopt, sortopt) → {Array}
- Source:
返回指定长度的升序/降序数组
Example
minN([1, 2, 3]) // [1]
minN([1, 2, 3], 2) // [1, 2]
minN([1, 2, 4, 3], 3, 'asc') // [1, 2, 3]
minN([1, 2, 4, 3], 3, 'desc') // [4, 3, 2]
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
arr |
Array | 目标数组 | ||
n |
number |
<optional> |
1
|
|
sort |
string |
<optional> |
"asc"
|
asc:升序;desc:降序 |
Returns:
- Type
- Array
negate(func) → {Array}
- Source:
根据条件反向筛选
Example
[1, 2, 3, 4, 5, 6].filter(arrayTool.negate((n => n % 2 === 0))); // [ 1, 3, 5 ]
Parameters:
Name | Type | Description |
---|---|---|
func |
function |
Returns:
- Type
- Array
nest(items, idopt, linkopt) → {Array}
- Source:
根据parent_id生成树结构(阿里一面真题)
Example
const comments = [
{ id: 1, parent_id: null },
{ id: 2, parent_id: 1 },
{ id: 3, parent_id: 1 },
{ id: 4, parent_id: 2 },
{ id: 5, parent_id: 4 }
];
nest(comments) // [{ id: 1, parent_id: null, children: [
{
id: 2,
parent_id: 1,
children: [
{
id:2,
parent_id: 1,
children: [{id: 4, parent_id: 2, children: [
{id: 5, parent_id: 4, children: [{id: 5, parent_id: 4, children: [] }]}
]
}]
}
]
},
{ id: 2, parent_id: 1, children: [] }
]}]
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
items |
Array | 目标数组 | ||
id |
* |
<optional> |
null
|
根据特定字段作为id |
link |
string |
<optional> |
"parent_id"
|
父id |
Returns:
- Type
- Array
randomIntArrayInRange(min, max, nopt) → {Array}
- Source:
生成两数之间指定长度的随机数组
Example
arrayTool.randomIntArrayInRange(10,20,10); // [11, 12, 10, 15, 18, 12, 15, 16, 13, 15]
arrayTool.randomIntArrayInRange(10,10,10); // [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
min |
number | 最小值 | ||
max |
number | 最大值 | ||
n |
number |
<optional> |
1
|
返回数组的长度 |
Returns:
- Type
- Array
sample(arr) → {Array}
- Source:
在指定数组中获取随机数
Example
sample([1,5,8,9,10]); // 10
sample([1,5,8,9,10]); // 5
sample([1,5,8,9,10]); // 1
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array | 目标数组 |
Returns:
- Type
- Array
sampleSize(nopt) → {Array}
- Source:
在指定数组中获取指定长度的随机数(“洗牌” 数组)
Example
sampleSize([1, 2, 3,4], 2); // [4, 3]
sampleSize([1, 2, 3,4], 2); // [1, 3]
sampleSize([1, 2, 3,4], 2); // [1, 4]
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
...arr |
Array |
<optional> |
||
n |
number |
<optional> |
1
|
Returns:
- Type
- Array
similarity(arr1, arr2) → {Array}
- Source:
数组交集(方法一)
Example
similarity([1,2,3],[5,2]) // [2]
Parameters:
Name | Type | Description |
---|---|---|
arr1 |
Array | |
arr2 |
Array |
Returns:
- Type
- Array
start()
- Source:
打印开始点
uniqueArray(arr) → {Array}
- Source:
原理:利用Set中不能出现重复元素的特征
Example
uniqueArray([undefined, null, null, 1, 1]) // [undefined, null, 1]
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array | 目标数组 |
Returns:
- Type
- Array
uniqueMultiDimensionalArray(arr) → {Array}
- Source:
对多维数组(矩阵)去重(方法一)
Example
uniqueMultiDimensionalArray([
["你的", "我", "它"],
["我", "你的", "它"],
["一", "二", "三"],
["三", "二", "一"],
["你d", "a", "它"],
["a", "你d", "它"],
["one", "two", "three"],
["three", "two", "one"]
]) // [ ["你的", "它", "我"], ["二", "三", "一"], ["你d", "它", "a"], ["one", "three", "two"] ]
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array |
Returns:
- Type
- Array
uniqueMultiDimensionalArray1(arr) → {Array}
- Source:
对多维数组(矩阵)去重(方法二)
Example
uniqueMultiDimensionalArray1([
["你的", "我", "它"],
["我", "你的", "它"],
["一", "二", "三"],
["三", "二", "一"],
["你d", "a", "它"],
["a", "你d", "它"],
["one", "two", "three"],
["three", "two", "one"]
]) // [ ["你的", "它", "我"], ["二", "三", "一"], ["你d", "它", "a"], ["one", "three", "two"] ]
Parameters:
Name | Type | Description |
---|---|---|
arr |
Array |
Returns:
- Type
- Array