在Javascript中生成二维数组

时间:2016-07-29 01:49:09

标签: javascript arrays d3.js

我想生成一个这样的数组:

layers = [
     [{"x1":0,"y":0.55,"y0":0},{"x1":1,"y":0.804,"y0":0}],
     [{"x2":0, "y":0.189,"y0":0.553},{"x2":1,"y":0.189,"y0":0.804}]
    ];

要在JavaScript中生成此内容,我会浏览整个CSV数据集并创建一个二维数组并将每个对象推入其中:

var layers = new Array(2);
    layers[0] = new Array();
    layers[1] = new Array();

d3.csv("data.csv", function(error, data) {
  if (error) throw error;

  var ageNames = ["x1", "x2"];

  data.forEach(function(d) {
    var y0 = 0;
    d.ages = ageNames.map(function(name) { return {name: d.x, y0: y0, y: y0 += +d[name]}; });
    layers[0].push(d.ages[0]);
    layers[1].push(d.ages[1]);
    });
  }); 

但似乎阵列结构与我原本想要的结构相同。任何解决方案?

0 个答案:

没有答案