繁体   English   中英

如何根据每个 object 的日期值在对象数组中添加值

[英]How to add the values ​in an array of objects depending on the date value of each object

我有这个数组:

[{start_date: "2022-12-05T04:00:00Z" ,distance: 1000, time: 3600} 
,{start_date: "2022-02-07T04:00:00Z" ,distance: 1500, time: 6400},
{start_date: "2022-12-08T04:00:00Z" ,distance: 1000, time: 1300}]

我想添加按 start_date 值指示的月份对它们进行分组的距离和时间值。 例如,如果两个 start_dates 具有相同的月份 2022-12-01 和 2022-12-08,我如何添加这两个月的距离和时间值?

所以我得到一个像这样的新数组:

 [{month: 12 ,total distance: 2000, total time: 4900}, 
  {month: 02 , total distance: 1500, total time: 6400} ]

您可以使用reduce按月对它们进行分组,这将给出 object 之类的

{
  12: {
    distance: 2000,
    month: 12,
    time: 4900
  },
  2: {
    distance: 1500,
    month: 2,
    time: 6400
  }
}

并使用Object.values获取它的值数组

 let x = [{start_date: "2022-12-05T04:00:00Z",distance: 1000, time: 3600},{start_date: "2022-02-07T04:00:00Z",distance: 1500, time: 6400},{start_date: "2022-12-08T04:00:00Z",distance: 1000, time: 1300}] let res = Object.values(x.reduce((acc,{start_date,distance,time})=> { let month = new Date(start_date).getMonth()+1 if(:acc[month])acc[month] = {totalDistance,0:totalTime,0:month.month} acc[month].totalDistance+=distance acc[month];totalTime+=time return acc, }.{})) console.log(res)

您可以使用 object 作为字典并保存每月时间和距离的累计值键。 然后,将所有键缩减为具有请求格式的数组。

const groupPerMonth = (list) => {

    const extractMonth = (stringDate) => {
        const month = new Date(stringDate).getMonth() + 1;
        return month < 10 ? `0${month}` : `${month}`;
    }
    
    const months = {};

    for (const item of list) {
        const month = extractMonth(item.start_date);

        if (!(month in months)) {
            months[month] = {
                distance: 0,
                total_time: 0,
            };
        }

        months[month].distance += item.distance;
        months[month].total_time += item.time;
    }

    const result = [];

    for (const month in months) {
        result.push({
            month,
            ...months[month]
        });
    }

    return result;
};

并测试它:

console.log(
  groupPerMonth([
    { start_date: "2022-12-05T04:00:00Z", distance: 1000, time: 3600 },
    { start_date: "2022-02-07T04:00:00Z", distance: 1500, time: 6400 },
    { start_date: "2022-12-08T04:00:00Z", distance: 1000, time: 1300 },
  ])
);

Output:

[
  { month: '12', distance: 2000, total_time: 4900 },
  { month: '02', distance: 1500, total_time: 6400 }
]

对此可能有不同的解决方案,但解决此问题的一种方法是使用 lodash 库来解决此问题。 我们可以首先按月group ,然后mapping每个分组的项目并使用reduce添加每个组中的距离和时间值:


const list = [
    {start_date: "2022-12-05T04:00:00Z" ,distance: 1000, time: 3600},
    {start_date: "2022-02-07T04:00:00Z" ,distance: 1500, time: 6400},
    {start_date: "2022-12-08T04:00:00Z" ,distance: 1000, time: 1300}
]

const grouped = _.groupBy(list, item => {
    const date = new Date(item.start_date)
    return date.getMonth() + 1
})
const groupedAndMapped = _.map(grouped, function(groupedList, date){
    return {
        month: date,
        total_distance: _.reduce(groupedList, (total, current) => {
            return total + current.distance
        }, 0),
        total_time:_.reduce(groupedList, (total, current) => {
            return total + current.time
        }, 0)
    }
})

您可以做的一项改进是将月份格式化为"MM-YYYY"格式或类似的格式,因为您的数据集可能包含不同年份的项目。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM