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


6个月前 280次点击 来自 前端

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

  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) => {
	...
});

Made with in Shangrao,China By Devler.

Copyright © Devler 2012 - 2022

赣ICP备19009883号-1

Top ↑