2

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']]
1
  • Well, you're constantly adding items to subArray through push so yeah, that item will keep growing for every push to superArray. What you're trying to do is var subArray = [i, x]; superArray.push(subArray); Basically what @ItsJohnB said. Commented Aug 4, 2015 at 15:02

2 Answers 2

1

You don't need the subArray.

Just use this one superArray.push([i, x]);

Sign up to request clarification or add additional context in comments.

1 Comment

You might want to add an explanation of why OP doesn't need subArray and how it's messing up their code. Your answer as is ended you up in the "review for deletion" queue.
0

I managed to fix this by adding:

subArray.length = 0;

After

superArray.push(subArray.slice(0));

1 Comment

While this may work, it's not the solution you're looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.