将json对象格式化为JS中的数组等电子表格

时间:2018-04-12 11:42:34

标签: javascript arrays json node.js

我试图在Json对象中重新格式化数据,我编写了简单的函数,奇怪的是新数组的所有行都具有相同的值。

 var array_json = [{"date_hour":{"value":"2018-04-12T12:12:00"},"avg_ech1":0,"avg_ech2":20,"avg_ech3":30,"avg_ech4":40,"avg_pm2_5":21,"avg_pm10":22,"data_points":1},{"date_hour":{"value":"2018-04-12T12:13:00"},"avg_ech1":10,"avg_ech2":20,"avg_ech3":30,"avg_ech4":40,"avg_pm2_5":21,"avg_pm10":22,"data_points":2},{"date_hour":{"value":"2018-04-12T12:14:00"},"avg_ech1":10,"avg_ech2":20,"avg_ech3":30,"avg_ech4":40,"avg_pm2_5":20,"avg_pm10":21,"data_points":2},{"date_hour":{"value":"2018-04-12T12:15:00"},"avg_ech1":10,"avg_ech2":20,"avg_ech3":30,"avg_ech4":40,"avg_pm2_5":19.5,"avg_pm10":20.5,"data_points":2}];

function json2array(json_array){
  var array_full = [["Date","ECH1","ECH2","ECH3","ECH4","PM2_5","PM10"]];
  //header in first line
  var array_line = [];

  for (var i=0; i<json_array.length;i++){
    array_line[0]=json_array[i].date_hour.value;
    array_line[1]=json_array[i].avg_ech1;
    array_line[2]=json_array[i].avg_ech2;
    array_line[3]=json_array[i].avg_ech3;
    array_line[4]=json_array[i].avg_ech4;
    array_line[5]=json_array[i].avg_pm2_5;
    array_line[6]=json_array[i].avg_pm10;
    console.log('line',array_line);
    array_full[i+1]=array_line;
    console.log('full line',array_full[i+1]);
    }
    return array_full;
}

console.log('test',json2array(array_json));

控制台输出: 您可以在输出中看到每一行看起来都很好但是在检查整个数组时,所有行都具有相同的值。我在这里错过了什么吗?

node v9.7.1 linux/amd64

line [ '2018-04-12T12:12:00', 0, 20, 30, 40, 21, 22 ]
full line [ '2018-04-12T12:12:00', 0, 20, 30, 40, 21, 22 ]
line [ '2018-04-12T12:13:00', 10, 20, 30, 40, 21, 22 ]
full line [ '2018-04-12T12:13:00', 10, 20, 30, 40, 21, 22 ]
line [ '2018-04-12T12:14:00', 10, 20, 30, 40, 20, 21 ]
full line [ '2018-04-12T12:14:00', 10, 20, 30, 40, 20, 21 ]
line [ '2018-04-12T12:15:00', 10, 20, 30, 40, 19.5, 20.5 ]
full line [ '2018-04-12T12:15:00', 10, 20, 30, 40, 19.5, 20.5 ]

test [ [ 'Date', 'ECH1', 'ECH2', 'ECH3', 'ECH4', 'PM2_5', 'PM10' ],
  [ '2018-04-12T12:15:00', 10, 20, 30, 40, 19.5, 20.5 ],
  [ '2018-04-12T12:15:00', 10, 20, 30, 40, 19.5, 20.5 ],
  [ '2018-04-12T12:15:00', 10, 20, 30, 40, 19.5, 20.5 ],
  [ '2018-04-12T12:15:00', 10, 20, 30, 40, 19.5, 20.5 ] ]

2 个答案:

答案 0 :(得分:0)

您为每个循环使用相同的对象引用。通过在循环内移动array_line = [];,可以创建一个独立的数组。

var array_line;                               // declare in advance
for (var i = 0; i < json_array.length; i++) {
    array_line = [];                          // initialize with empty array

    // assignemnts
}

答案 1 :(得分:0)

在for循环中创建一个新变量。当您在循环外创建对象并推送到数组时,所有数组内容都引用相同的对象,并且每当对象更改时,所有数组值都将被更改。

&#13;
&#13;
function json2array(json_array){
  var array_full = [["Date","ECH1","ECH2","ECH3","ECH4","PM2_5","PM10"]];
  //header in first line

  for (var i=0; i<json_array.length;i++){
       var array_line = [];
    array_line[0]=json_array[i].date_hour.value;
    array_line[1]=json_array[i].avg_ech1;
    array_line[2]=json_array[i].avg_ech2;
    array_line[3]=json_array[i].avg_ech3;
    array_line[4]=json_array[i].avg_ech4;
    array_line[5]=json_array[i].avg_pm2_5;
    array_line[6]=json_array[i].avg_pm10;
    console.log('line',array_line);
    array_full[i+1]=array_line;
    console.log('full line',array_full[i+1]);
    }
    return array_full;
}


var array_json = []; // initialize your data
console.log('test', json2array(array_json));
&#13;
&#13;
&#13;