Array.from(arr) 和[...arr] 均可以將類數組arr轉換成數組
什麼叫類數組javascript
若是arr只有索引和長度,而且是對象,因此能夠被Array.from轉換成數組的,可是[...arr]方法,就必須能夠被迭代java
let obj = {'0': 1,'1': 2,'2': 3,length: 3} let arr = Array.from(obj) console.log(arr) let arr1= [...obj] console.log(arr1)
object is not iterable
,因此咱們若是須要在obj上進行...運算,還須要在上面增長一個屬性[Symbol.iterator]let obj = { '0': 1, '1': 2, '2': 3, length: 4, [Symbol.iterator]: function(){ let index = 0 let next = () => { return { value: this[index], done: this.length == ++ index } } return { next } } } let arr = Array.from(obj) console.log(arr) let arr1= [...obj] console.log(arr1)