如何对包含多个日期的对象数组中的日期进行排序

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

How to sort the dates from different objects with multiples dates inside of an array of object

问题

我已经翻译好了代码部分:

我有这个从API中收集的对象数组

const schedules = [
  {
      "_id": "6436b48b875967d0bea245b4",
      "service": "64246dc9a2d61593d103c749",
      "scheduledDates": [
          {
              "date": "2023-04-17T18:00:00.000Z",
              "user": "643701f7e5f61e6760f4d1f3"
          },
          {
              "date": "2023-04-12T18:00:00.000Z",
              "user": "643701f7e5f61e6760f4d1f3"
          }
      ]
  },
  {
      "_id": "6436b48b875967d0bea245b5",
      "service": "64246dc9a2d61593d103c749",
      "scheduledDates": [
          {
              "date": "2023-04-19T10:30:00.000Z",
              "user": "64217a8dcc69c5fa48a5b484"
          },
          {
              "date": "2023-04-12T18:00:00.000Z",
              "user": "6414be936b0bbf2bd8fa964f"
          }
      ]
  },
]

你要如何按照scheduledDates数组内的日期对这个数组进行排序,并保持相同的对象结构,使其看起来像这个数组:

const newSchedules = [
  {
      "_id": "6436b48b875967d0bea245b4",
      "service": "64246dc9a2d61593d103c749",
      "scheduledDates": [
          {
              "date": "2023-04-12T18:00:00.000Z",
              "user": "643701f7e5f61e6760f4d1f3"
          }
      ]
  },
  {
    "_id": "6436b48b875967d0bea245b5",
    "service": "64246dc9a2d61593d103c749",
    "scheduledDates": [
        {
            "date": "2023-04-12T18:00:00.000Z",
            "user": "6414be936b0bbf2bd8fa964f"
        }
    ]
},
  {
      "_id": "6436b48b875967d0bea245b4",
      "service": "64246dc9a2d61593d103c749",
      "scheduledDates": [
          {
              "date": "2023-04-17T18:00:00.000Z",
              "user": "643701f7e5f61e6760f4d1f3"
          }
      ]
  },
  {
      "_id": "6436b48b875967d0bea245b5",
      "service": "64246dc9a2d61593d103c749",
      "scheduledDates": [
          {
              "date": "2023-04-19T10:30:00.000Z",
              "user": "64217a8dcc69c5fa48a5b484"
          }
      ]
  },
]

你有这个用于处理排序的函数:

const handleSort = () => {
  if (sortOrder === "asc") {
    setSortOrder("desc");
    return schedules.map((schedule) =>
    schedule.scheduledDates.sort((a, b) => new Date(a.date) - new Date(b.date))
    );
  } else {
    setSortOrder("asc");
    return schedules.map((schedule) =>
    schedule.scheduledDates.sort((a, b) => new Date(b.date) - new Date(a.date))
  }
};

但它只在每个scheduledDates数组内部进行排序。希望对你有所帮助。

英文:

I have this array of objects that I gather from an API:

  const schedules = [
    {
        "_id": "6436b48b875967d0bea245b4",
        "service": "64246dc9a2d61593d103c749",
        "scheduledDates": [
            {
                "date": "2023-04-17T18:00:00.000Z",
                "user": "643701f7e5f61e6760f4d1f3"
            },
            {
                "date": "2023-04-12T18:00:00.000Z",
                "user": "643701f7e5f61e6760f4d1f3"
            }
        ]
    },
    {
        "_id": "6436b48b875967d0bea245b5",
        "service": "64246dc9a2d61593d103c749",
        "scheduledDates": [
            {
                "date": "2023-04-19T10:30:00.000Z",
                "user": "64217a8dcc69c5fa48a5b484"
            },
            {
                "date": "2023-04-12T18:00:00.000Z",
                "user": "6414be936b0bbf2bd8fa964f"
            }
        ]
    },
]

How can I sort this array by the date inside schedulesDates array, keep the same array of objects structure, looking like this array:

const newSchedules = [
  {
      "_id": "6436b48b875967d0bea245b4",
      "service": "64246dc9a2d61593d103c749",
      "scheduledDates": [
          {
              "date": "2023-04-12T18:00:00.000Z",
              "user": "643701f7e5f61e6760f4d1f3"
          }
      ]
  },
  {
    "_id": "6436b48b875967d0bea245b5",
    "service": "64246dc9a2d61593d103c749",
    "scheduledDates": [
        {
            "date": "2023-04-12T18:00:00.000Z",
            "user": "6414be936b0bbf2bd8fa964f"
        }
    ]
},
  {
      "_id": "6436b48b875967d0bea245b4",
      "service": "64246dc9a2d61593d103c749",
      "scheduledDates": [
          {
              "date": "2023-04-17T18:00:00.000Z",
              "user": "643701f7e5f61e6760f4d1f3"
          }
      ]
  },
  {
      "_id": "6436b48b875967d0bea245b5",
      "service": "64246dc9a2d61593d103c749",
      "scheduledDates": [
          {
              "date": "2023-04-19T10:30:00.000Z",
              "user": "64217a8dcc69c5fa48a5b484"
          }
      ]
  },
]

I have this function to handle the sort:

  const handleSort = () => {
    if (sortOrder === "asc") {
      setSortOrder("desc");
      return schedules.map((schedule) =>
      schedule.scheduledDates.sort((a, b) => new Date(a.date) - new Date(b.date))
      );
    } else {
      setSortOrder("asc");
      return schedules.map((schedule) =>
      schedule.scheduledDates.sort((a, b) => new Date(b.date) - new Date(a.date))
    }
  };

But it sorts only within each array of scheduledDates. Thanks for helping.

答案1

得分: 2

你可以使用 flatMapmap 来展开数组。使用 map 是因为我假设每个 scheduledDates 可能包含不固定数量的数据,而不仅仅是2个。如果确实是固定的2个,那么不需要使用 map,只需获取第[0]和[1]个元素并返回。接下来是通常的排序。我不明白为什么最终结果的 scheduledDates 应该只包含一个元素的数组。更好的方法是使用具有 dateuser 属性的对象。不过,将其更改为这种形式不会很困难。

如果您需要进一步的翻译或解释,请随时告诉我。

英文:

you could use flatMap and map to unwind the array. used map since I assumed that each scheduledDates can have a dynamic amount of data not just 2. If it is fixed to 2 don't use map just get the [0]th,[1]th and return.
Next is the usual sorting. I don't understand why the final result should have an array of just 1 element for scheduledDates. A better approach would be to have an object with date,user properties. Anyway changing to that wont be difficult

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

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

const schedules = [   {        &quot;_id&quot;: &quot;6436b48b875967d0bea245b4&quot;,        &quot;service&quot;: &quot;64246dc9a2d61593d103c749&quot;,        &quot;scheduledDates&quot;: [            {                &quot;date&quot;: &quot;2023-04-17T18:00:00.000Z&quot;,                &quot;user&quot;: &quot;643701f7e5f61e6760f4d1f3&quot;            },            {                &quot;date&quot;: &quot;2023-04-12T18:00:00.000Z&quot;,                &quot;user&quot;: &quot;643701f7e5f61e6760f4d1f3&quot;            }        ]    },    {        &quot;_id&quot;: &quot;6436b48b875967d0bea245b5&quot;,        &quot;service&quot;: &quot;64246dc9a2d61593d103c749&quot;,        &quot;scheduledDates&quot;: [           {                &quot;date&quot;: &quot;2023-04-19T10:30:00.000Z&quot;,                &quot;user&quot;: &quot;64217a8dcc69c5fa48a5b484&quot;            },            {                &quot;date&quot;: &quot;2023-04-12T18:00:00.000Z&quot;,                &quot;user&quot;: &quot;6414be936b0bbf2bd8fa964f&quot;            }        ]    },]

const res = schedules
  .flatMap(e =&gt; e.scheduledDates.map(d =&gt; ({...e,scheduledDates:[d]})))
  .sort((a, b) =&gt; new Date(a.scheduledDates[0].date) - new Date(b.scheduledDates[0].date))

console.log(res)

<!-- end snippet -->

scheduledDates as an object
<!-- begin snippet: js hide: false console: true babel: false -->

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

const schedules = [   {        &quot;_id&quot;: &quot;6436b48b875967d0bea245b4&quot;,        &quot;service&quot;: &quot;64246dc9a2d61593d103c749&quot;,        &quot;scheduledDates&quot;: [            {                &quot;date&quot;: &quot;2023-04-17T18:00:00.000Z&quot;,                &quot;user&quot;: &quot;643701f7e5f61e6760f4d1f3&quot;            },            {                &quot;date&quot;: &quot;2023-04-12T18:00:00.000Z&quot;,                &quot;user&quot;: &quot;643701f7e5f61e6760f4d1f3&quot;            }        ]    },    {        &quot;_id&quot;: &quot;6436b48b875967d0bea245b5&quot;,        &quot;service&quot;: &quot;64246dc9a2d61593d103c749&quot;,        &quot;scheduledDates&quot;: [           {                &quot;date&quot;: &quot;2023-04-19T10:30:00.000Z&quot;,                &quot;user&quot;: &quot;64217a8dcc69c5fa48a5b484&quot;            },            {                &quot;date&quot;: &quot;2023-04-12T18:00:00.000Z&quot;,                &quot;user&quot;: &quot;6414be936b0bbf2bd8fa964f&quot;            }        ]    },]

const res = schedules
  .flatMap(e =&gt; e.scheduledDates.map(d =&gt; ({...e,scheduledDates:d})))
  .sort((a, b) =&gt; new Date(a.scheduledDates.date) - new Date(b.scheduledDates.date))

console.log(res)

<!-- end snippet -->

答案2

得分: 1

如果您想根据scheduledDates中的日期对此对象数组进行排序,您需要修改排序函数。您可以在计划数组上使用sort()方法,并传递一个比较函数来比较scheduledDates数组中的日期。

可以这样工作:

const handleSort = () => {
  if (sortOrder === "asc") {
    setSortOrder("desc");
    schedules.sort((a, b) => {
      return new Date(a.scheduledDates[0].date) - new Date(b.scheduledDates[0].date);
    });
  } else {
    setSortOrder("asc");
    schedules.sort((a, b) => {
      return new Date(b.scheduledDates[0].date) - new Date(a.scheduledDates[0].date);
    });
  }
};
英文:

If you want to sort this array of objects based on the dates in scheduledDates, you have to modify your sorting function. You can use the sort() method on the schedules array and pass a comparison function that compares the dates in the scheduledDates arrays.

It should work this way:

const handleSort = () =&gt; {
  if (sortOrder === &quot;asc&quot;) {
    setSortOrder(&quot;desc&quot;);
    schedules.sort((a, b) =&gt; {
      return new Date(a.scheduledDates[0].date) - new Date(b.scheduledDates[0].date);
    });
  } else {
    setSortOrder(&quot;asc&quot;);
    schedules.sort((a, b) =&gt; {
      return new Date(b.scheduledDates[0].date) - new Date(a.scheduledDates[0].date);
    });
  }
};

huangapple
  • 本文由 发表于 2023年4月13日 22:53:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006904.html
匿名

发表评论

匿名网友

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

确定