JavaScript中的Array

JavaScript中的Array

数组的创建

创建数组

const colors = new Array();
const colors = new Array(20);
const colors = new Array("red", "blue", "yellow");

不使用new关键字

const colors = Array(20);
const colors = Array("red", "blue", "yellow");

这些数组创建等价于[]

数组的转换

map集合转换

const map = new Map();
map.set(1, 2);
map.set("Bunny", 16);
console.log(Array.from(map));// [ [ 1, 2 ], [ 'Bunny', 16 ] ]

使用set转换

const set = new Set();
set.add(1);
set.add("bunny");
set.add(16);
console.log(Array.from(set));// [ 1, 'bunny', 16 ]

Array是浅复制

const a = [1, 2, 3, 4, 5, 6, 7, 8];
const a1 = Array.from(a);
console.log(a == a1);// false