Converting a JS object to an array using jQuery

后端 未结 18 3438
鱼传尺愫
鱼传尺愫 2020-11-22 01:37

My application creates a JavaScript object, like the following:

myObj= {1:[Array-Data], 2:[Array-Data]}

But I need this object as an array.

18条回答
  •  我寻月下人不归
    2020-11-22 02:08

    If you know the maximum index in you object you can do the following:

    var myObj = {
        1: ['c', 'd'],
        2: ['a', 'b']
      },
      myArr;
    
    myObj.length = 3; //max index + 1
    myArr = Array.prototype.slice.apply(myObj);
    console.log(myArr); //[undefined, ['c', 'd'], ['a', 'b']]

提交回复
热议问题