类数组对象转化为数组 Uncaught TypeError: xxx.forEach is not a function
6个月前 • 280次点击 • 来自 前端
类数组对象转化为数组后才可被遍历,可使用以下方法转换:
- Array.from()
- Array.prototype.slice.call()
- [...elems]
- Array.prototype.forEach.call(elem,callback)
- Array.prototype.forEach.apply(elem,[callback])
- 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) => {
...
});