0

In C#, I would use the following Dictionary

Dictionary << Date, int []>>

for having an array bound to a particular date. What would be the simplest way in JS to achieve the same?

I need to be able to alter the values in the integer array.

John V
  • 4,855
  • 15
  • 39
  • 63
  • you can create something almost the same in JS. : https://stackoverflow.com/questions/7196212/how-to-create-dictionary-and-add-key-value-pairs-dynamically/22315575 – Budyn Nov 12 '18 at 16:34
  • You can use the JavaScript `Map` class, but be warned that equality comparison for Map keys is less flexible than in Java (and I would strongly suspect C#). – Pointy Nov 12 '18 at 16:34
  • 1
    just remember IE 8 < does not support Map function – Budyn Nov 12 '18 at 16:35

3 Answers3

2

Use the timestamp of that date as a key for a js object.

For example, let's say you want to map date a to array b:

const a = new Date();
const b = [1, 2, 3];


const c = {
  [a.getTime()]: b,
};

in this case, c would be an object or hash map from date to array.

rubentd
  • 1,245
  • 11
  • 25
  • 1
    In particular with Date instances this is not a bad idea, because the timestamp comparison is reliable and works with different Date instances. That's probably more like what the OP would expect. – Pointy Nov 12 '18 at 16:36
1

You can achieve an equal behaviour in Js in various ways.

Dicitionary could be replaced by a Map, or just an object ({}) with dynamic keys, however if you use an object you can only use strings as keys, anything else will be converted to a string. There is also a Date object in JS, however two dates representing the same date in time do not match:

 const a = new Date(0), b = new Date(0);
 console.log(a === b); // false

therefore they would not get stored in the same entry in the Map. Instead you could just stringify the Date, as strings are conpared by value. the int[] would just be an array of numbers in js.

  const date1 = new Date(0);
  const date1b = new Date(0);

 const obj = {};
 const map = new Map;

 obj[date1] = [1, 2, 3];
 // equals:
 obj[date1.toString()] = [1, 2, 3];

 map.set(date1, [1, 2, 3]);
 map.set(date1.toString(), [3, 4, 5]);

 console.log(
   obj[date1b], // [1, 2, 3]
   map.get(date1b), // undefined
   map.get(date1b.toString()), // [3, 4, 5]
 );

I only need to group by days

In that case, you'd have to generate a number or string out of the Date that only contains the day:

 (new Date()).toString().split("T")[0] // 2018-12-11

Then use that string as the Maps / objects key.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • I retrieve dates from the database and using Moment when storing them. – John V Nov 12 '18 at 16:45
  • @John then i guess the timestamp is already a string (or a number?) in that case just take that. – Jonas Wilms Nov 12 '18 at 16:47
  • May I ask - I realized the date is also saved with its time component but I only need to group by days, how can I only use the date component? I guess otherwise I will never have two records under the same key as the milliseconds will not match :) – John V Nov 12 '18 at 16:51
1

Since you mentioned you are using momentjs, you could use the moment unix or toDate().getTime() to to get the date in ms and then utilize either an object or Map to get what you need:

let date1 = moment().unix()
let date2 = moment().add(7, 'days').unix()

let dict = {}  // using object

dict[date1] = [1,2,3]
dict[date2] = [4,5,6]

console.log('dict via Object', dict[date1], dict[date2])

let dict2 = new Map()  // using Map

dict2.set(date1, [1,2,3])
dict2.set(date2, [4,5,6])

console.log('dict via Map', dict2.get(date1), dict2.get(date2))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Akrion
  • 18,117
  • 1
  • 34
  • 54