1.1合并数组
const a1 = [1, 2, 3], a2 = [4], a3 = [5, 6];
const b = [...a1, ...a2, ...a3] // [1, 2, 3, 4, 5, 6]
1.2解构赋值
const [first, ...rest] = [1, 2, 3, 4];
first // 1
rest // [2, 3, 4]
1.3将字符串转换为数组
let obj = [...'hello']
console.log(obj) // [ 'h', 'e', 'l', 'l', 'o' ]
2.1类数组对象转换成数组
let obj2 = {
0:'zhangsan',
1:'lisi',
2:'wangwu',
length:3
}
let arr2 = Array.from(obj2)
console.log(arr2); //[ 'zhangsan', 'lisi', 'wangwu' ]
// 只能迭代可迭代的对象,可迭代的数据
// es5 Array.prototype.slice.call('类数组对象',0)
2.2字符串转换为数组
let obj = 'hello'
let arr1 = Array.from(obj)
console.log(arr1); //[ 'h', 'e', 'l', 'l', 'o' ]
let arr3 = Array.of(3,4,'tom') //当前所创建数组的第一个元素
console.log(arr3); // [ 3, 4, 'tom' ]
//Array.of()不存在因为参数的不同导致的重载,基本上可以代替Array()或new Array()