Converting a JS object to an array using jQuery

后端 未结 18 3067
鱼传尺愫
鱼传尺愫 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 01:56

    Nowadays, there is a simple way to do this : Object.values().

    var myObj = {
        1: [1, 2, 3],
        2: [4, 5, 6]
    };
    
    console.log(Object.values(myObj));
    

    Output:

    [[1, 2, 3], [4, 5, 6]]
    

    This doesn't required jQuery, it's been defined in ECMAScript 2017.
    It's supported by every modern browser (forget IE).

提交回复
热议问题