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]

大象
211 声望6 粉丝