(五十二)ES6 - 数组的扩展
1:数组的扩展
扩展运算符 - 将一个数组转为用逗号分隔的参数序列
console.log(1, ...[2, 3, 4], 5);
/* 1 2 3 4 5 6 */
const array1 = [ 1, 2 ];
const array2 = [ ...array1 ];
[...arr1, ...arr2, ...arr3];
const [first, ...rest] = [1, 2, 3, 4, 5];
first
/* 1 */
rest
/* [2, 3, 4, 5] */
[...'hello']
/* [ "h", "e", "l", "l", "o" ] */
[...'abcabcabc'.matchAll('ab')];
let nodeList = document.querySelectorAll('div');
let array = [...nodeList];
Array.from - 类数组(包含length)的对象和迭代器对象转换为数组
let arrayLike = {
'0': 'a',
'1': 'b',
'2': 'c',
length: 3
};
Array.from(arrayLike);
/* ['a', 'b', 'c'] */
let divList = document.querySelectorAll('div');
Array.from(divList);
Array.from([1, 2, 3])
/* [1, 2, 3] */
Array.from({ length: 3 });
/* [ undefined, undefined, undefined ] */
Array.from([1, 2, 3], (x) => x * x);