类数组转化为数组

2020-10-12  本文已影响0人  砚婉儿

类数组是具有length属性,但不具有数组原型上的方法。
常见的类数组有arguments、DOM操作方法返回的结果。

方法:


// 方法一:Array.from
Array.from(document.querySelectorAll('div'))

// 方法二:Array.prototype.slice.call()
Array.prototype.slice.call(document.querySelectorAll('div'))

// 方法三:扩展运算符
[...document.querySelectorAll('div')]

上一篇 下一篇

猜你喜欢

热点阅读