How to map an array to a date?

typescript sort array by date descending
moment js sort array by date
javascript array of dates
lodash sort by date
sort array by date php
javascript filter array of objects by date
javascript sort by month and year
sort date string array

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.


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.

How to sort an array by date value in JavaScript, You want to sort those activities by the date property. You can use the sort() method of Array , which takes a callback function, which takes as  .map() can be used to iterate through objects in an array and, in a similar fashion to traditional arrays, modify the content of each individual object and return a new array. This modification is done based on what is returned in the callback function.


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,
};

JavaScript Array sort: Sorting an Array More Effectively, This tutorial shows you how to use the JavaScript array sort method to sort arrays However, it is just a string that represents a valid date, not the Date object. 1; }); // copy element back to the array var sortedRivers = lengths.map(function (e)  The map() method creates a new array with the results of calling a function for every array element. The map() method calls the provided function once for each element in an array, in order. Note: map() does not execute the function for array elements without values. Note: this method does not change the original array.


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>

Sort an Object Array by Date in JavaScript, How to convert Object's array to an array using JavaScript ? PranchalKatiyar. Check out this Author's contributed articles. If you like GeeksforGeeks and would like  Convert ArrayList To DateTime array Hi, Can anybody tell me how can I convert a ArrayList to an array of DateTime. I have code like DateTime[] newDat. I'll cover the following topics in the code samples below: IFormatProviderSqlDataReader, Page, ToDateTime, EventArgs, and ArrayList.


How to select Min/Max dates in an array using JavaScript , How to select Min/Max dates in an array using JavaScript ? Given an array of JavaScript date. The task is to get the minimum and maximum date of the array using  This example use the Apache Commons Lang’s ArrayUtils.toMap() method to convert a two dimensional array into a Map object.. To convert a two dimensional array into a Map object, each element of the two dimensional array must be an array with at least two elements where the first element will be the key and the second element will be the value.


How To Generate an Array of Months, Years, or Days in JavaScript, diff(Moment|String|Number|Date|Array, String, Boolean);. The function accepts three different parameters, and two are optional. First parameter. In this scenario, we know there is no repeated elements in the source data, but this technique helps to convert the source ArrayList object into an Object (which is a Map in whose key is the source column ID and the value is an array containing the attributes in each row from the source data).


Javascript sorting arrays by date, I'll show you how to do it then explain it. import moment from 'moment'const dateArray = [{date:"2018-05-11"},{date:"2018  It’s important to note that the map() method does not change the original array, it creates a new array of all elements that have been transformed by the callback function. More JavaScript Array map() examples. The following example shows how to transform an array of numbers by using a built-in method of the Math type as the callback() function.