【发布时间】:2021-04-20 23:35:19
【问题描述】:
我有一个播客应用的对象数组,如下所示:
[{ id: "uuid-1"
timeInSeconds: 1000
dateListened: "2021-01-01T15:57:17.000Z" }, // <---same day
{ id: "uuid-2"
timeInSeconds: 4900
dateListened: "2021-01-01T16:57:17.000Z" }, // <---same day
{ id: "uuid-3"
timeInSeconds: 3200
dateListened: "2021-09-01T16:57:17.000Z" },
{ id: "uuid-4"
timeInSeconds: 6000
dateListened: "2021-10-01T16:57:17.000Z" } ]
如果dateListened 在同一天,我想创建一个结合活动时间的函数。我希望它看起来像这样:
[{ id: "uuid-new"
timeInSeconds: 5900 // <---- combined seconds listened on Jan 1
dateListened: "2021-01-01T15:57:17.000Z" },
{ id: "uuid-3"
timeInSeconds: 3200
dateListened: "2021-09-01T16:57:17.000Z" },
{ id: "uuid-4"
timeInSeconds: 6000
dateListened: "2021-10-01T16:57:17.000Z" } ]
我最接近的尝试是使用带有嵌套 .some() 的 .map() 但我仍然远远不够,甚至不值得发布我的尝试。有没有人对下一步尝试什么方向有任何提示或想法?谢谢!
【问题讨论】:
-
那么您需要多少帮助?你只需要像
isSameDay(date1, date2)这样的函数吗? -
你的 json 不好
-
有很多dupes 可供选择。请注意,
map在这里不起作用,因为它总是返回与输入相同数量的元素。要更改输出数量,您通常会使用reduce。
标签: javascript arrays ecmascript-6