如何按月份分组而不失去按年排序?

huangapple go评论68阅读模式
英文:

How can I group by month without losing the order by year?

问题

尝试按年份和月份对数据进行排序以在图表中显示。首先按年份排序,然后按月份排序很顺利,但一旦尝试根据月份将每个项目分组在一起,年份的排序就会丢失。

假设我有一个对象数组如下所示:

const arr = [
    {month: 1, year: 2021},
    {month: 2, year: 2021},
    {month: 3, year: 2021},
    {month: 3, year: 2021},
    {month: 4, year: 2021},
    {month: 9, year: 2020},
    {month: 9, year: 2020},
    {month: 10, year: 2020},
    {month: 11, year: 2020},
    {month: 12, year: 2020},
    {month: 12, year: 2020},
];

然后,我根据年份和月份排序,得到以下结果:

[
  {month: 9, year: 2020},
  {month: 9, year: 2020},
  {month: 10, year: 2020},
  {month: 11, year: 2020},
  {month: 12, year: 2020},
  {month: 12, year: 2020},
  {month: 1, year: 2021},
  {month: 2, year: 2021},
  {month: 3, year: 2021},
  {month: 3, year: 2021},
  {month: 4, year: 2021}
]

接下来,如何将对象分组到一个每个月的数组中,同时不丢失这个顺序?尝试最终得到类似以下对象的结果。每次尝试按月份分组时,它都会根据月份进行排序,因此月份1最先出现,这会破坏图表。

const groupedUp = {
  9: [{month: 9, year: 2020},{month: 9, year: 2020}],
  10: [{month: 10, year: 2020}],
  11: [{month: 11, year: 2020}],
  12: [{month: 12, year: 2020}, {month: 12, year: 2020}],
  1: [{month: 1, year: 2021}],
  2: [{month: 2, year: 2021}],
  3: [{month: 3, year: 2021},{month: 3, year: 2021}],
  4: [{month: 4, year: 2021}]
}
英文:

Trying to sort data for display in a graph by the year and the month category. Sorting first by year and then by month goes great, but then as soon as I try to group each item together based on month, the year sorting goes out the window.

Let's say that I have an array of objects as follows

const arr = [
        {month: 1, year: 2021},
        {month: 2, year: 2021},
        {month: 3, year: 2021},
        {month: 3, year: 2021},
        {month: 4, year: 2021},
        {month: 9, year: 2020},
        {month: 9, year: 2020},
        {month: 10, year: 2020},
        {month: 11, year: 2020},
        {month: 12, year: 2020},
        {month: 12, year: 2020},
      ];

I then sort based on year and then month so I get this resulting arr:

[
  {month: 9, year: 2020},
  {month: 9, year: 2020},
  {month: 10, year: 2020},
  {month: 11, year: 2020},
  {month: 12, year: 2020},
  {month: 12, year: 2020},
  {month: 1, year: 2021},
  {month: 2, year: 2021},
  {month: 3, year: 2021},
  {month: 3, year: 2021},
  {month: 4, year: 2021}
]

How can I then group the objects into an array of each month while not losing this order? Trying to end up with something like the following Object. Every time I try to group by month it ends up sorting it based on month and so Month: 1 ends up coming first which screws up the graph.

const groupedUp = {
  9: [{month: 9, year: 2020},{month: 9, year: 2020}],
  10: [{month: 10, year: 2020}],
  11: [{month: 11, year: 2020}],
  12: [{month: 12, year: 2020}, {month: 12, year: 2020}],
  1: [{month: 1, year: 2021}],
  2: [{month: 2, year: 2021}],
  3: [{month: 3, year: 2021},{month: 3, year: 2021}],
  4: [{month: 4, year: 2021}]
}

答案1

得分: 2

在对象中,数值属性总是按升序排序。您可以使用 Map 代替,以在分组时保持插入顺序。

let sorted = [
  {month: 9, year: 2020},
  {month: 9, year: 2020},
  {month: 10, year: 2020},
  {month: 11, year: 2020},
  {month: 12, year: 2020},
  {month: 12, year: 2020},
  {month: 1, year: 2021},
  {month: 2, year: 2021},
  {month: 3, year: 2021},
  {month: 3, year: 2021},
  {month: 4, year: 2021}
];
let res = sorted.reduce((map, curr) => {
  if (!map.has(curr.month)) map.set(curr.month, []);
  map.get(curr.month).push(curr);
  return map;
}, new Map);
res.forEach((v, k) => console.log(k, '=', JSON.stringify(v)));
英文:

In an object, numeric properties are always sorted in ascending order. You can use a Map instead to maintain insertion order when grouping.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let sorted = [
  {month: 9, year: 2020},
  {month: 9, year: 2020},
  {month: 10, year: 2020},
  {month: 11, year: 2020},
  {month: 12, year: 2020},
  {month: 12, year: 2020},
  {month: 1, year: 2021},
  {month: 2, year: 2021},
  {month: 3, year: 2021},
  {month: 3, year: 2021},
  {month: 4, year: 2021}
];
let res = sorted.reduce((map, curr) =&gt; {
  if (!map.has(curr.month)) map.set(curr.month, []);
  map.get(curr.month).push(curr);
  return map;
}, new Map);
res.forEach((v, k) =&gt; console.log(k, &#39;=&#39;, JSON.stringify(v)));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月11日 03:49:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定