簡體   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