-1

I have a problem with my script Javascript. I try to create a multidimensional array with dates in the key.

I would like to have something like

"1" : [ "2018-01-01", "2018-02-02" ]
"11" : [ "2018-01-01", "2018-02-02" ]  

But I get something like

1 : [ "2018-01-01", "2018-02-02" ]
2 : ""
3 : ""
..
11 : [ "2018-01-01", "2018-02-02" ]

I'm doing

var array = [];
array[no].push(date);

Thanks for the future help,
MYT.

3
  • 1
    Please post your input array. Commented May 14, 2018 at 9:42
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. Commented May 14, 2018 at 9:44
  • Search sparse vs dense arrays javascript Commented May 14, 2018 at 9:49

1 Answer 1

1

You should use an Object rather than an Array.

var object = {};
object[no] = object[no] || [];
object[no].push(date);
Sign up to request clarification or add additional context in comments.

1 Comment

I remember have tested it, but I made probably a mix that make it not work. Thanks it was that c:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.