ES6之Array.from()方法

2019-03-04  本文已影响0人  墨芊baby
let arr = [1,2,3,4,5];
let arr2 = Array.from(arr);
//相当于let arr2 = [...arr]
console.log(arr)        // [1,2,3,4,5]
console.log(arr2)        // [1,2,3,4,5]
let array = {
    0: 'name', 
    1: 'age',
    2: 'sex',
    3: ['user1','user2','user3'],
    'length': 4
}
let arr = Array.from(array )
console.log(arr) // ['name','age','sex',['user1','user2','user3']]
let array = {
    'name': 'name', 
    'age': 'age',
    'sex': 'sex',
    'user': ['user1','user2','user3'],
    'length': 4
}
let arr = Array.from(array )
console.log(arr)  // [ undefined, undefined, undefined, undefined ]
let array = [1,2,3,1,2,4]
let arr = Array.from(new Set(array)) // [1,2,3,4]
let str =  'hello'
let arr = Array.from(str)   // ["h", "e", "l", "l", "o"]
//相当于str.split('')
let arr = [1,2,3,4,5]
let set = new Set(arr)
console.log(Array.from(set, item => item + 1)) // [2,3,4,5,6]
上一篇 下一篇

猜你喜欢

热点阅读