As part of some changes I've been asked to make I'm replacing a cshtml with a jsx file, so part of that includes rewriting some C# functions in a js equivalent way. One of the ones I am trying to do at the moment is:
Dictionary<int, List<DraftComment>> GroupComments(List<DraftComment> allComments)
{
return allComments.Where(c => !c.IsDeleted)
.OrderByDescending(c => c.Time)
.GroupBy(c => c.MinorRevision)
.ToDictionary(g => g.Key, g => g.ToList());
}
I have started to do this and have got to this point so far:
const filteredComments = allComments.
filter(obj => obj.IsDeleted === false).
sort((objA, objB) => new Date(objB.Time) - new Date(objA.Time)).
reduce((group, revision) => {
const { MinorRevision } = revision;
group[MinorRevision] = group[MinorRevision] ?? [];
group[MinorRevision].push(revision);
return group;
}, {});
which is giving me this:
{0: Array(39), 1: Array(2), 2: Array(2)}
0: (39) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
1: (2) [{…}, {…}]
2: (2) [{…}, {…}]
[[Prototype]]: Object
Now the part I am not sure about is to then do a js equivalent of:
.ToDictionary(g => g.Key, g => g.ToList());
Which what I am thinking if I can get working, I will then be able to do something along the lines of:
filteredComments.forEach((element, index, array) => {
console.log(element.Commenter.Name);
console.log(index);
console.log(array);
});
So I suppose my question would be, what would be the js equivalent of that .ToDictionary line? Plus any feedback on my js code so far would be welcomed