-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.

Victor M Perez
  • 2,185
  • 3
  • 19
  • 22
mytDRAGON
  • 59
  • 12
  • 1
    Please post your input array. – Mihai Alexandru-Ionut May 14 '18 at 09: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](https://stackoverflow.com/help/mcve). – Praveen Kumar Purushothaman May 14 '18 at 09:44
  • Search sparse vs dense arrays javascript – mplungjan May 14 '18 at 09:49

1 Answers1

1

You should use an Object rather than an Array.

var object = {};
object[no] = object[no] || [];
object[no].push(date);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59