mardi 4 août 2015

JSON encoding array

I have a jQuery graph which builds the x-axis like so:

xaxis: {
  tickColor: 'transparent',
  tickDecimals: 0,
  ticks: [[1,'27/07'],[2,'28/07'],[3,'29/07'],[4,'30/07'],[5,'31/07'],[6,'01/08'],[7,'02/08']]
},

I want the 'ticks' to be generated by a piece of javascipt that loops between 2 variable dates like so:

var i = 1;
var superArray = [];
var subArray = []; 

for (var d = d1; d <= d2; d.setDate(d.getDate() + 1)) {

  var m0 = d.getMonth() + 1;
  var d0 = d.getDate();

  m0 = m0 > 9 ? m0 : "0"+m0;
  d0 = d0 > 9 ? d0 : "0"+d0;

  var x = d0 + '/' + m0;

  subArray.push(i, x);
  superArray.push(subArray.slice(0));

  i++;

}

console.log(JSON.stringify(superArray));

The console.log looks like so:

[[1,"27/07"],[1,"27/07",2,"28/07"],[1,"27/07",2,"28/07",3,"29/07"],[1,"27/07",2,"28/07",3,"29/07",4,"30/07"],[1,"27/07",2,"28/07",3,"29/07",4,"30/07",5,"31/07"],[1,"27/07",2,"28/07",3,"29/07",4,"30/07",5,"31/07",6,"01/08"],[1,"27/07",2,"28/07",3,"29/07",4,"30/07",5,"31/07",6,"01/08",7,"02/08"]]

Which is kinda close to what I want but not quite!

How can I make this:

[[1,"27/07"],[1,"27/07",2,"28/07"],[1,"27/07",2,"28/07",3,"29/07"],[1,"27/07",2,"28/07",3,"29/07",4,"30/07"],[1,"27/07",2,"28/07",3,"29/07",4,"30/07",5,"31/07"],[1,"27/07",2,"28/07",3,"29/07",4,"30/07",5,"31/07",6,"01/08"],[1,"27/07",2,"28/07",3,"29/07",4,"30/07",5,"31/07",6,"01/08",7,"02/08"]]

Look like this:

[[1,'27/07'],[2,'28/07'],[3,'29/07'],[4,'30/07'],[5,'31/07'],[6,'01/08'],[7,'02/08']]



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire