简体   繁体   English

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

[英]How to combine like objects within array of objects and insert new object?

I have an array of objects for a podcast app that looks like this:我有一个播客应用程序的对象数组,如下所示:

[{ 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" } ]

I want to create a function that combines activity times if the dateListened is on the same day.如果dateListened在同一天,我想创建一个结合活动时间的 function。 I want it to look something like this:我希望它看起来像这样:

[{ 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" } ]

My closest attempts have been to use a.map() with a nested.some() but I'm still far off enough that it's not even worth posting my tries.我最接近的尝试是将 a.map() 与 nested.some() 一起使用,但我仍然远远不够,甚至不值得发布我的尝试。 Does anyone have any hints or ideas of what direction to try next?有没有人对下一步尝试什么方向有任何提示或想法? Thank you!谢谢!

You could reduce the items and map them to a date key, retrieve the values, and then map them to single objects.您可以将项目和 map 减少为日期键,检索值,然后将 map 减少为单个对象。

You can key on the following:您可以键入以下内容:

let date = new Date(info.dateListened).toLocaleDateString('en-US')

 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);
 .as-console-wrapper { top: 0; max-height: 100%;important; }

You can achieve the result using reduce and only add the timeInSeconds if its year , month , and date is exactly the same/equal.您可以使用reduce获得结果,并且仅在其yearmonthdate完全相同/相等时才添加timeInSeconds

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将对象数组组合成一个 object,然后根据索引条件在该数组中创建另一个 object - How to combine array of objects into one object and then create another object within that array based on a condition on the index 如何使用对象中的对象数组的值创建一个新数组? - How to create a new array using the values of an array of objects within objects? 如何在JavaScript中将两个不同的对象数组合并为一个新的对象数组? - how to combine two different array of objects into one new array of object in javascript? 如何将两个类似 arrays 的对象组合成一个数组 object? - How do you combine two like arrays of objects into a single array object? 将一组对象组合成一个 OBJECT - Combine an array of objects into one OBJECT 如何在对象数组中插入 object - How can I insert an object in an array of objects 将新的内部对象插入对象可观察对象数组并更新DOM - Insert a new inner object into array of objects observable array and update the DOM 对象,数组或类似对象的数组 - Object, Array or Array like objects 如何将数组中的 object 拆分为多个对象? - How to split an object within an array into multiple objects? 使用 useState 钩子在对象数组中修改并插入新的 object - modify and insert new object in array of objects with useState hook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM