Converting a JS object to an array using jQuery

后端 未结 18 3063
鱼传尺愫
鱼传尺愫 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:04

    If you want to keep the name of the object's properties as values. Example:

    var fields = {
        Name: { type: 'string', maxLength: 50 },
        Age: { type: 'number', minValue: 0 }
    }
    

    Use Object.keys(), Array.map() and Object.assign():

    var columns = Object.keys( fields ).map( p => Object.assign( fields[p], {field:p} ) )
    

    Result:

    [ { field: 'Name', type: 'string', maxLength: 50 }, 
      { field: 'Age', type: 'number', minValue: 0 } ]
    

    Explanation:

    Object.keys() enumerates all the properties of the source ; .map() applies the => function to each property and returns an Array ; Object.assign() merges name and value for each property.

    0 讨论(0)
  • 2020-11-22 02:05

    Fiddle Demo

    Extension to answer of bjornd .

    var myObj = {
        1: [1, [2], 3],
        2: [4, 5, [6]]
    }, count = 0,
        i;
    //count the JavaScript object length supporting IE < 9 also
    for (i in myObj) {
        if (myObj.hasOwnProperty(i)) {
            count++;
        }
    }
    //count = Object.keys(myObj).length;// but not support IE < 9
    myObj.length = count + 1; //max index + 1
    myArr = Array.prototype.slice.apply(myObj);
    console.log(myArr);
    


    Reference

    Array.prototype.slice()

    Function.prototype.apply()

    Object.prototype.hasOwnProperty()

    Object.keys()

    0 讨论(0)
  • 2020-11-22 02:07

    After some tests, here is a general object to array function convertor:

    You have the object:

    var obj = {
        some_key_1: "some_value_1"
        some_key_2: "some_value_2"
    };
    

    The function:

    function ObjectToArray(o)
    {
        var k = Object.getOwnPropertyNames(o);
        var v = Object.values(o);
    
        var c = function(l)
        {
            this.k = [];
            this.v = [];
            this.length = l;
        };
    
        var r = new c(k.length);
    
        for (var i = 0; i < k.length; i++)
        {
            r.k[i] = k[i];
            r.v[i] = v[i];
        }
    
        return r;
    }
    

    Function Use:

    var arr = ObjectToArray(obj);
    

    You Get:

    arr {
        key: [
            "some_key_1",
            "some_key_2"
        ],
        value: [
            "some_value_1",
            "some_value_2"
        ],
        length: 2
    }
    

    So then you can reach all keys & values like:

    for (var i = 0; i < arr.length; i++)
    {
        console.log(arr.key[i] + " = " + arr.value[i]);
    }
    

    Result in console:

    some_key_1 = some_value_1
    some_key_2 = some_value_2
    

    Edit:

    Or in prototype form:

    Object.prototype.objectToArray = function()
    {
        if (
            typeof this != 'object' ||
            typeof this.length != "undefined"
        ) {
            return false;
        }
    
        var k = Object.getOwnPropertyNames(this);
        var v = Object.values(this);
    
        var c = function(l)
        {
            this.k = [];
            this.v = [];
            this.length = l;
        };
    
        var r = new c(k.length);
    
        for (var i = 0; i < k.length; i++)
        {
            r.k[i] = k[i];
            r.v[i] = v[i];
        }
    
        return r;
    };
    

    And then use like:

    console.log(obj.objectToArray);
    
    0 讨论(0)
  • 2020-11-22 02:08

    Simply do

    Object.values(obj);
    

    That's all!

    0 讨论(0)
  • 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']]

    0 讨论(0)
  • 2020-11-22 02:08

    ECMASCRIPT 5:

    Object.keys(myObj).map(function(x) { return myObj[x]; })
    

    ECMASCRIPT 2015 or ES6:

    Object.keys(myObj).map(x => myObj[x])
    
    0 讨论(0)
提交回复
热议问题