类数组对象转化为数组 Uncaught TypeError: xxx.forEach is not a function

3周前 62次点击 来自 前端

类数组对象转化为数组后才可被遍历,可使用以下方法转换:

  1. Array.from()
  2. Array.prototype.slice.call()
  3. [...elems]
  4. Array.prototype.forEach.call(elem,callback)
  5. Array.prototype.forEach.apply(elem,[callback])
  6. Array.prototype.forEach.bind(elemlist)
let list = Array.from(somelist);

let list = Array.prototype.slice.call(somelist);

let list = [...somelist];

Array.prototype.forEach.call(somelist,function(){
	...
})

Array.prototype.map.call(somelist,function(){
	...
})

Array.prototype.map.call(somelist,[(current,index) => {
	...
}]);

Array.prototype.forEach.bind(somelist)((current,index) => {
	...
});
Card image cap
开发者雷

尘世间一个小小的开发者,每天增加一些无聊的知识,就不会无聊了

要加油~~~

技术文档 >> 系列应用 >>
热推应用
Let'sLearnSwift
学习Swift的入门教程
PyPie
Python is as good as Pie
标签