内置对象的扩展-2

  1. 1. 扩展运算符
  2. 2. Array.form()
  3. 3. Array.of()
  4. 4. copyWithin()
  5. 5. find 相关
    1. 5.1. find()
    2. 5.2. findIndex()
    3. 5.3. find() 和 findLastIndex()
  6. 6. fill()
  7. 7. 遍历数组
  8. 8. includes()
  9. 9. 数组扁平化
  10. 10. at()
  11. 11. 排序

摘要:数组的扩展方法

扩展运算符

扩展运算符...可以将一个数组转换为用逗号分隔的参数序列。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 用于函数调用
let items = [1,2,3,4,5];
function push(array, ...items){
array.push(...items);
}

// 复制数组
const a1 = [1, 2];
const a2 = [...a1]; // 写法一
const [...a2] = a1; // 写法二

// 合并数组
const arr1 = ['a', 'b'];
const arr2 = ['c'];
const arr3 = ['d', 'e'];
[...arr1, ...arr2, ...arr3];

// 转化字符串
[...'hello']; // ['h', 'e', 'l', 'l', 'o']

Array.form()

Array.from()方法可以将类似数组的对象array-like object和可遍历iterable的对象转为真正的数组。

1
2
3
4
5
6
7
8
9
10
11
let arrayLike = {
'0': 'a',
'1': 'b',
'2': 'c',
length: 3
};
let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']

function test(){
console.log(Array.from(arguments))
}

Array.of()

Array.of()方法用于将一组值,转换为数组,主要目的是弥补数组构造函数Array()的不足。
Array()方法没有参数、一个参数、三个参数时,返回的结果都不一样。
只有当参数不少于 2 个时,Array()才会返回由参数组成的新数组。只有一个正整数时,实际上是指定数组的长度。

1
2
3
4
5
6
7
8
9
Array() // []
Array(3) // [, , ,]
Array(3, 11, 8) // [3, 11, 8]

Array.of(3, 11, 8) // [3,11,8]
Array.of(3) // [3]
Array.of(3).length // 1
Array.of() // []
Array.of(undefined) // [undefined]

copyWithin()

数组实例的copyWithin()方法,在当前数组内部,将指定位置的成员复制到其他位置,然后返回当前数组。使用这个方法,会修改当前数组。

1
2
3
4
5
6
7
8
Array.prototype.copyWithin(target, start = 0, end = this.length)
- target: 从该位置开始替换数据。如果为负值,表示倒数。
- start: 从该位置开始读取数据,默认为 0。如果为负值,表示从末尾开始计算。
- end: 到该位置前停止读取数据,默认等于数组长度。如果为负值,表示从末尾开始计算。

[1, 2, 3, 4, 5].copyWithin(0, 3); // [4, 5, 3, 4, 5]
[1, 2, 3, 4, 5].copyWithin(0, 3, 4) // [4, 2, 3, 4, 5]
[1, 2, 3, 4, 5].copyWithin(0, -2, -1) // [4, 2, 3, 4, 5]

find 相关

find()

find()方法用于找出第一个符合条件的数组成员。它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的成员,然后返回该成员。如果没有符合条件的成员,则返回undefined。接受三个参数,依次为当前的值、当前的位置和原数组。

1
2
3
[1, 5, 10, 15].find(function(value[, index, arr]) {
return value > 9;
}) // 10

findIndex()

findIndex()方法的用法与find()方法非常类似,返回第一个符合条件的数组成员的位置,如果所有成员都不符合条件,则返回-1

1
2
3
[1, 5, 10, 15].findIndex(function(value, index, arr) {
return value > 9;
}) // 2

这两个方法都可以接受第二个参数,用来绑定回调函数的this对象。

1
2
3
4
5
function f(v){
return v > this.age;
}
let person = {name: 'John', age: 20};
[10, 12, 26, 15].find(f, person); // 26

find() 和 findLastIndex()

这两个方法与前两个一致,但是是从数组的最后一个成员开始,依次向前检查

fill()

fill()方法可以使用给定的值填充一个数组,调用后会改变原数组。接受第二个和第三个参数,用于指定填充的起始位置和结束位置。

1
2
3
['a', 'b', 'c'].fill(7) // [7, 7, 7]
new Array(3).fill(7) // [7, 7, 7]
['a', 'b', 'c'].fill(7, 1, 2) // ['a', 7, 'c'],表示从1的位置开始,到2的位置之前结束。

遍历数组

entries() —— 遍历键值对

keys() —— 遍历键名

values() —— 遍历键值

1
2
3
4
5
6
7
8
9
10
11
12
13
let arr = ['a', 'b'];

for(let index of arr.keys()){
console.log(index); // 0 , 1
}

for(let item of arr.values()){
console.log(item); // 'a' , 'b'
}

for(let [index,item] of arr.entries()){
console.log(index,item);// 0 'a' , 1 'b'
}

includes()

includes方法返回一个布尔值,表示某个数组是否包含给定的值,与字符串的includes方法类似。
该方法的第二个参数表示搜索的起始位置,有以下注意点:

  • 默认为0
  • 参数为负数,则表示倒数的位置
  • 参数大于数组长度,则会重置为从0开始
1
2
3
4
5
[1, 2, 3].includes(2)     // true
[1, 2, 3].includes(4) // false
[1, 2, NaN].includes(NaN) // true
[1, 2, 3].includes(3, 3); // false
[1, 2, 3].includes(3, -1); // true

数组扁平化

Array.prototype.flat()用于将嵌套的数组“拉平”,变成一维的数组,返回一个新数组。

  • flat()接受一个参数,表示“拉平”嵌套数组的层数,默认为1层。
  • 无视嵌套,均转化为一维数组,传递 Infinity。
  • 原数组有空位,flat()方法会跳过空位。
1
2
3
4
[1, 2, [3, [4, 5]]].flat() // [1, 2, 3, [4, 5]]
[1, 2, [3, [4, 5]]].flat(2) // [1, 2, 3, 4, 5]
[1, [2, [3]]].flat(Infinity) // [1, 2, 3]
[1, 2, , 4, 5].flat() // [1, 2, 4, 5]

at()

at()方法,接受一个整数作为参数,返回对应位置的成员,支持负索引。用于数组,字符串和类型数组

1
2
3
4
5
6
const arr = [5, 12, 8, 130, 44];
arr.at(2) // 8
arr.at(-2) // 130
const sentence = 'This is a sample sentence';
sentence.at(0); // 'T'
sentence.at(-1); // 'e'

排序

sort()方法可以对数组进行排序,默认按照ASC码进行排序。可通过传递一个函数改变排序规则。

1
2
3
4
5
6
7
8
9
10
11
12
let a=[1,5,9,6,3,4,15,11];
a.sort() // [1, 11, 15, 3, 4, 5, 6, 9]

// 升序
a.sort((a,b)=>{
return a-b
}) // [1, 3, 4, 5, 6, 9, 11, 15]

// 降序
a.sort((a,b)=>{
return b-a
}) // [15, 11, 9, 6, 5, 4, 3, 1]
true