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

前端 未结 7 1254
南方客
南方客 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 17:04
    function toObjects (array) {
      var new_array = [];
      var headers = [];
    
      for(var i = 0; i < array.length; i++){
        var obj = {};
        for(var j = 0; j < array[i].length; j++){
          if(i === 0){
            headers.push(array[i][j]);
          } else {
            obj[headers[j]] = array[i][j];
          }
        }
        if(i > 0){
          new_array.push(obj);
        }
      }
      return new_array;
    }
    
    0 讨论(0)
提交回复
热议问题