Array.from()
作用:
从一个类似数组或可迭代对象创建一个新的,浅拷贝的数组实例。
语法
Array.from(arrayLike[, mapFn[, thisArg]])
参数:
- arrayLike 想要转换成数组的伪数组对象或可迭代对象。
- mapFn 可选,如果指定了该参数,新数组中的每个元素会执行该回调函数。
- thisArg 可选,可选参数,执行回调函数 mapFn 时 this 对象
返回值:
一个新的数组实例。
实例
1.把一个字符串变成一个数组
var t="today";
console.log(Array.from(t));//["t", "o", "d", "a", "y"]
2.把一个Set类型的对象变成Array数组。
const myset=new Set(['lisa','coco','marray','hop']);
console.log(Array.from(myset));// ["lisa", "coco", "marray", "hop"]
3.把一个Map类型的对象变成Array二维数组。
const map = new Map([
['name', 'lisa'],
['age', 12],
['address', 'beijing']
]);
console.log(Array.from(map));