首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在对象数组中组合相似的对象并插入新对象?

如何在对象数组中组合相似的对象并插入新对象?
EN

Stack Overflow用户
提问于 2021-04-20 23:35:19
回答 2查看 37关注 0票数 0

我有一个用于播客应用程序的对象数组,如下所示:

代码语言:javascript
复制
[{ 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在同一天,我想要创建一个结合活动时间的函数。我想让它看起来像这样:

代码语言:javascript
复制
[{ 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" } ]

我最近的尝试是使用嵌套的.map()和嵌套的.some(),但我仍然远远不够,甚至不值得发布我的尝试。有没有人对下一步要尝试的方向有任何提示或想法?谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-21 00:20:15

您可以减少项目并将它们映射到日期键,检索值,然后将它们映射到单个对象。

您可以键入以下内容:

代码语言:javascript
复制
let date = new Date(info.dateListened).toLocaleDateString('en-US')

代码语言:javascript
复制
const data = [
  { 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" }
 ];

const result = Object
  .values(data.reduce((acc, info) =>
    (date =>
      ({ ...acc, [date]: [...(acc[date] || []), info ] }))
    (new Date(info.dateListened).toLocaleDateString('en-US')), {}))
  .map(value => value.length === 1 ? value : {
    id: 'uuid-new',
    timeInSeconds: value.reduce((sum, { timeInSeconds }) => sum + timeInSeconds, 0),
    dateListened: value[0].dateListened
  });

console.log(result);
代码语言:javascript
复制
.as-console-wrapper { top: 0; max-height: 100% !important; }

票数 1
EN

Stack Overflow用户

发布于 2021-04-21 00:19:33

您可以使用reduce实现结果,并且只有在其yearmonthdate完全相同/相等的情况下才能添加timeInSeconds

代码语言:javascript
复制
const arr = [
  {
    id: "uuid-1",
    timeInSeconds: 1000,
    dateListened: "2021-01-01T15:57:17.000Z",
  },
  {
    id: "uuid-2",
    timeInSeconds: 4900,
    dateListened: "2021-01-01T16: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",
  },
];

const result = arr.reduce((acc, curr) => {
  const { id, timeInSeconds, dateListened } = curr;
  const searchSameDateElement = acc.find((o) => {
    const first = new Date(o.dateListened);
    const second = new Date(dateListened);
    const isYearSame = first.getFullYear() === second.getFullYear();
    const isMonthSame = first.getMonth() === second.getMonth();
    const isDateSame = first.getDate() === second.getDate();
    return isYearSame && isMonthSame && isDateSame;
  });
  if (!searchSameDateElement) {
    acc.push(curr);
  } else {
    searchSameDateElement.timeInSeconds += timeInSeconds;
  }
  return acc;
}, []);

console.log(result);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67187536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档