共计 675 个字符,预计需要花费 2 分钟才能阅读完成。
Array.form
- 作用 :将数组、类数组、带有索引和 length 属性的对象、Set 对象、Map 对象、字符串转换为实在数组
- 参数 :第一个参数为要转换的数据(必选)、第二个参数为 function, 相似 map 办法(可选)。
- 返回值 :返回解决后的新数组
示例:
let obj = {
"0":"a",
"1":"b",
"2":"c",
"length":3
}
console.log(Array.from(obj));//["a", "b", "c"]
let arr = [0,1,2];
let set = new Set(arr);
console.log(Array.from(set));//[0, 1, 2]
let map = new Map();
map.set("name","Archer");
map.set("age","18");
console.log(Array.from(map));//[["name", "Archer"],["age", "18"]]
let str = "Archer";
console.log(Array.from(str));//["A", "r", "c", "h", "e", "r"]
let Arr = [0,1,2,3];
var Arr2 = Array.from(Arr);
console.log(Arr2);//[0,1,2,3]
console.log(Arr == Arr2);//false
let Arr3 = [0,1,2];
let Arr4 = Array.from(Arr3,(item,index) =>{return item + 1;})
console.log(Arr4);//[1, 2, 3]
正文完