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

第一个借用数组的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)

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理