按日期对对象值进行分组 [英] Group object values by date

查看:58
本文介绍了按日期对对象值进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 React Native 上举办一个即将到来的活动,我有这个对象

I'm trying to make an upcoming event on react native, I have this object

const calendatData = [
        {
            "data": [
              {
                "id": 25,
                "title": "Spotify",
                "name": "Family",
                "service_id": 1,
                "repetition": "Monthly",
                "price": "79000",
                "upcoming_date": [
                  "2020-08-07T13:35:44.606Z"
                ]
              },
              {
                "id": 26,
                "title": "Netflix",
                "name": "Mobile",
                "service_id": 2,
                "repetition": "Monthly",
                "price": "49000",
                "upcoming_date": [
                  "2020-08-18T13:35:44.600Z",
                  "2020-08-07T13:35:44.606Z"
                ]
              },
              {
                "id": 27,
                "title": "iTunes",
                "name": "Weekly Special",
                "service_id": 3,
                "repetition": "Weekly",
                "price": "22000",
                "upcoming_date": [
                  "2020-08-07T13:35:44.606Z",
                  "2020-08-14T13:35:44.606Z",
                  "2020-08-21T13:35:44.606Z",
                  "2020-08-28T13:35:44.606Z"
                ]
              }
            ],
            "status": "success"
          }
    ]

我一直试图做的是根据 upcoming_date 提取该对象.我需要的结果是这样的

what I've been trying to do is to extract that object based on the upcoming_date. the result that I need is like this

upcomingData = [
        {
            date: '2020-08-07',
            title: [
                'Spotify',
                'Netflix',
                'iTunes'
            ]
        },
        {
            date: '2020-08-18',
            title: ['Netflix']
        },
        {
            date: '2020-08-14',
            title: ['iTuunes']
        },
        {
            date: '2020-08-21',
            title: ['iTuunes']
        },
        {
            date: '2020-08-28',
            title: ['iTuunes']
        }
]

在同一日期,如果有多个标题,则应将其分组在对象中的同一日期下.

On the same date, if there are multiple titles, it should be grouped under the same date in the object.

相反,我得到的是这个对象

instead what I got was this object

upcomingData = [
    {
        title: [
            "Spotify",
            "Netflix",
            "iTunes",
        ],
        date : [
            "2020-08-29",
            "2020-08-07",
            "2020-08-18",
            "2020-08-07",
            "2020-08-07",
            "2020-08-14",
            "2020-08-21",
            "2020-08-28",
        ]
    }
]

我是新手,我知道这主要是关于 javascript 知识,任何帮助将不胜感激.

I am new to this, and I'm aware that this is mostly about javascript knowledge, any help would be appreciated.

谢谢

推荐答案

你可以

以下是一个快速的现场演示:

Following is a quick live demo:

const src = [{"data":[{"id":25,"title":"Spotify","name":"Family","service_id":1,"repetition":"Monthly","price":"79000","upcoming_date":["2020-08-07T13:35:44.606Z"]},{"id":26,"title":"Netflix","name":"Mobile","service_id":2,"repetition":"Monthly","price":"49000","upcoming_date":["2020-08-18T13:35:44.600Z","2020-08-07T13:35:44.606Z"]},{"id":27,"title":"iTunes","name":"Weekly Special","service_id":3,"repetition":"Weekly","price":"22000","upcoming_date":["2020-08-07T13:35:44.606Z","2020-08-14T13:35:44.606Z","2020-08-21T13:35:44.606Z","2020-08-28T13:35:44.606Z"]}],"status":"success"}],
    
    result = [...src[0].data
      .reduce((r,{upcoming_date, title}) => (
         upcoming_date.forEach((s,_,__,date=s.slice(0,10)) =>
            r.set(
              date, 
              {date, title: [...(r.get(date)?.title||[]), title]}
            )),r),
         new Map())
      .values()
    ]
    
console.log(result)

.as-console-wrapper{min-height:100%;}

这篇关于按日期对对象值进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆