Array.from()
Array.from() 伪数组转数组
1、 将对象转为数组
2、将set结构转为数组
let arr = [1,1,2,6,6,7,8,6]; let set = new Set(arr); console.log(set); //{1,2,6,7,8} let arr1 = Array.from(set); console.log(arr1); //[1,2,6,7,8]
3、将map结构转为数组
4、字符串转数组
var str = "123456"; var str1 = "say hello"; console.log(Array.from(str)); //['1', '2', '3', '4', '5', '6'] console.log(Array.from(str1)); //['s', 'a', 'y', ' ', 'h', 'e', 'l', 'l', 'o']
5、实现数组拷贝
var oldArr = [1,2,3]; var newArr = Array.from(oldArr); console.log(newArr); // [1,2,3] console.log(oldArr == newArr); //false