ES6数组新增了哪些扩展?

扩展运算符

ES6通过扩展运算符

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

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

[...document.querySelectorAll('div')]
//[<div>,<div>,<div>]

主要用于函数调用的时候,将一个数组变为参数序列

function push(array,...items){
    array.push(...items);
}

function add(x,y){
    return x+y;
}

const numbers = [1,2];
add(...numbers) //3

可以将某些数据结构转为数组
[...document.querySelectorAll('div')]
能够更简单的实现数组复制和数组合并

//复制
const a1 = [1,2]
const [...a1] = a1;


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

通过扩展运算符实现的是浅拷贝,修改了引用指向的值,会同步反应到新数组

const arr1 = ['a','b',[1,2]];
const arr2 = ['c'];
const arr3 = [...arr1,arr2];
arr1[2][0] = 9999;//修改arr1里面数组成员值
console.log(arr[3]) // 影响到arr3,['a','b',[9999,2],'c']

扩展运算符可以与解构赋值结合起来,用于生成数组

const [first, ...rest] = [1, 2, 3, 4, 5];
first // 1
rest  // [2, 3, 4, 5]

const [first, ...rest] = [];
first // undefined
rest  // []

const [first, ...rest] = ["foo"];
first  // "foo"
rest   // []

如果将扩展运算符用于数组赋值,只能放在参数的最后一位,否则会报错

const [...butLast, last] = [1, 2, 3, 4, 5];
// 报错

const [first, ...middle, last] = [1, 2, 3, 4, 5];
// 报错

可以将字符串转为真正的数组

[...'hello']
// [ "h", "e", "l", "l", "o" ]

构造函数新增的方法

关于构造函数,数组新增的方法有如下:

  • Array.from()
  • Array.of()

Array.from()

将两类对象转为真正的数组:类似数组的对象和可遍历的对象

let arrayLike = {
    '0': 'a',
    '1': 'b',
    '2': 'c',
    length: 3
};
let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']

还可以接受第二个参数,用来对每个元素进行处理,将处理后的值放入返回的数组

Array.from([1, 2, 3], (x) => x * x)
// [1, 4, 9]

Array.of()

用于将一组值,转换为数组

Array.of(3,11,9) //[3,11,9]

没有参数的时候,返回一个空数组

当参数只有一个的时候,实际上是指定数组的长度

参数个数不少于 2 个时,Array()才会返回由参数组成的新数组

实例对象新增的方法

  • copyWithin()
  • find(),findIndex()
  • fill()
  • entries(),keys(),values()
  • includes()
  • flat(),flatMap()

copyWithin()

将制定位置的成员复制到其他位置(会覆盖原有成员),然后返回当前数组
参数如下:

  • target(必选):从该位置开始替换数据,如果为负值,表示倒数。
  • start (可选): 从该位置开始读取数据,默认为0,如果为负值,标识从末位开始计算。
  • end(可选): 到该位置前停止数据读取,默认等于数组长度,如果为负值,标识从末位开始计算。
[1,2,3,4,5,6].copyWithin(0,3) //将从3 号位置知道数组结束,复制到从0开始的位置,结果覆盖了原来的1和2,3
//[4,5,6,4,5,6]

find(),findIndex()

find()用于找出第一个符合条件的数组成员
参数是一个回调函数,接受三个参数一次为当前值,当前的位置和原数组

[1,5,10,15].find((value,index,arr)=> return value>9); // 10

findIndex()返回第一个符合条件的数组成员的位置,如果所有成员都不符合条件,则返回-1

[1,5,10,15].findIndex((value,index,arr)=> return value>9) // 2

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

function f(v){
    return v>this.age
}
let person = {name:'张三',age:20};
[10,12,26,15].find(f,person);//26

fill()

使用给定值,填充数组

['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']

如果填充的是对象,则是浅拷贝

entries(),keys(),values()

keys()是对键名的遍历,values()是对键值的遍历,entries()是对键值对的遍历

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

for(let elem of ['a','b'].values()){
  console.log(elem)
}
//'a'
//'b'

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

includes()

用户判断数组是否包含给定的值

[1, 2, 3].includes(2)     // true
[1, 2, 3].includes(4)     // false
[1, 2, NaN].includes(NaN) // true

方法的第二个参数表示搜索的起始位置,默认为0

参数为负数则表示倒数的位置

[1, 2, 3].includes(3, 3);  // false
[1, 2, 3].includes(3, -1); // true

flat(),flatMap()

将数组扁平化数理,返回一个新数组,对元数据没有影响

[1,2,[3,4]].flat();
//[1,2,3,4]

flat()默认只会“拉平”一层,如果想要“拉平”多层的嵌套数组,可以将flat()方法的参数写成一个整数,表示想要拉平的层数,默认为1

[1, 2, [3, [4, 5]]].flat()
// [1, 2, 3, [4, 5]]

[1, 2, [3, [4, 5]]].flat(2)
// [1, 2, 3, 4, 5]

flatMap()方法对原数组的每个成员执行一个函数相当于执行Array.prototype.map(),然后对返回值组成的数组执行flat()方法。该方法返回一个新数组,不改变原数组

// 相当于 [[2, 4], [3, 6], [4, 8]].flat()
[2, 3, 4].flatMap((x) => [x, x * 2])
// [2, 4, 3, 6, 4, 8]

flatMap()方法还可以有第二个参数,用来绑定遍历函数里面的this

排序稳定性

sort()默认设置为稳定的排序算法

const arr = [
 'peach',
 'straw',
 'apple',
 'spork'
];

const stableSorting = (s1,s2) => {
  if(s1[0] < s2[0]) return -1;
  return 1;
}

arr.sort(stableSorting)
// ["apple", "peach", "straw", "spork"]

排序结果中,strawspork的前面,跟原始顺序一致

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容