到的HTMLCollection转换成数组最有效的方法 [英] Most efficient way to convert an HTMLCollection to an Array

查看:1116
本文介绍了到的HTMLCollection转换成数组最有效的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有的HTMLCollection转换成数组,除通过的内容进行迭代更有效的方式表示收集和手动按每个项目到一个数组?


解决方案

  VAR ARR = Array.prototype.slice.call(的HTMLCollection)

将使用本地code有同样的效果。

修改:由于这得到了很多的意见,说明(每@奥里奥尔的评论),下面的更简洁的前pression相当于:

  VAR ARR = [] .slice.call(的HTMLCollection);

Is there a more efficient way to convert an HTMLCollection to an Array, other than iterating through the contents of said collection and manually pushing each item into an array?

解决方案

var arr = Array.prototype.slice.call( htmlCollection )

will have the same effect using "native" code.

EDIT: Since this gets a lot of views, note (per @oriol's comment) that the following more concise expression is equivalent:

var arr = [].slice.call(htmlCollection);

这篇关于到的HTMLCollection转换成数组最有效的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆