[removed] convert two dimensional array to array of objects using the first 'row' to define properties

前端 未结 7 1300
南方客
南方客 2020-12-03 16:33

In order to populate a data-grid that receives array of row objects, I am looking for a good solution to convert an array such as this:

[  
[\'country\', \'p         


        
7条回答
  •  被撕碎了的回忆
    2020-12-03 16:50

    You would need to use a bit of iteration to do this! The following code is an untested example to demonstrate what you would have to do.

    function convertToObjectArray(table)
    {
        var output = [];
    
        for(var i = 1; i < table.length; i++)
        {
            var obj = {};
            for(var x = 0; x < table[0].length; x++)
             obj[table[0][x]] = table[i][x];
    
             output.push(obj);
        }
    
        return output;
    }
    

    Another note with this example is you should edit this, however, to make sure the subsequent arrays are the same length or you could run into null values.

提交回复
热议问题