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

前端 未结 7 1301
南方客
南方客 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:54

    var array = [  
        ['country', 'population'],
        ['someplace', 100],
        ['otherplace', 200]
    ];
    
    var keys = array.shift();
    var objects = array.map(function(values) {
        return keys.reduce(function(o, k, i) {
            o[k] = values[i];
            return o;
        }, {});
    });
    

提交回复
热议问题