类数组对象转换为数组

78 阅读1分钟

类数组是具有length属性,但不具有数组原型上的方法

ES6语法 使用扩展运算符 。。。

let list = [...hdList];//用[ ...elems ]方法转化为数组并用list接收

使用 apply 和 Array.from 和 Array.prototype.slice.call

let objArr = {
    0:'12',
    1:'13',
    2:'241',
    length : 3
}

let list = [].concat.apply([],objArr);
console.log(list);   // ["12", "13", "241"]

let list = Array.from(objArr);
console.log(list);   // ["12", "13", "241"]

let list = Array.prototype.slice.call(objArr)
console.log(list);   // ["12", "13", "241"]