将伪数组转换为数组的几个方法

4次阅读

共计 338 个字符,预计需要花费 1 分钟才能阅读完成。

第一个借用数组的 slice 方法

var a ={
        0:'t',
        1:'a',
        2:'r',
        length:3
    }
    let b=Array.prototype.slice.call(a);
    console.log(b)
 请输入代码 

第二个 ES6 新增的一个方法

  var a ={
        0:'t',
        1:'a',
        2:'s',
        length:3
      }

      let b=Array.from(a)
      console.log(b)

第三个 原型

 var a ={
        0:'t',
        1:'a',
        2:'rr',
        length:3
      }
a.__proto__ = Array.prototype
console.log(a)

将对象转换为数组

var obj = {
            a: 1,
            b: 2,
            c: 3
        };
var newObj=Object.entries(obj);
console.log(newObj)

正文完
 0