Home > OS >  How to group objects date to days wise in javascript
How to group objects date to days wise in javascript

Time:11-07

I am getting below response from api . Below is sample respone not real Each object has createdDate , I have to group value is days wise like on 2022-09-20 (Tuesday) there are 2 customerId created and on 2022-09-21 (Wednesday) only one.

{
  "data": {
    "fetchOrdersAndRequest": [
      {
        "id": "PI786971",
        "customerId": [
          "C200147"
        ],
        "createdDate": "2022-09-21T04:46:00.126Z",
        }
        
        {
        "id": "PI786969",
        "customerId": [
          "C200146"
        ],
        "createdDate": "2022-09-20T04:46:00.126Z",
        }
        
        {
        "id": "PI786968",
        "customerId": [
          "C200145"
        ],
        "createdDate": "2022-09-20T04:46:00.126Z",
        }
        
        ]
        }
        }

I have to convert the response like , Tuesday I have 2 response so it should come in tue array and Wedensday one so only one and rest emply , but if data come for other days also it should filled in there days .

 {
    period:'lastWeek',
    data:[
        {
            "key":"SUN",
            "value" : []
        },
        {
            "key":"MON",
            "value" : []
        },
        {
            "key":"TUE",
            "value" : [{},{}],
        },
        {
            "key":"WED",
            "value" : [{}],
        },
        {
            "key":"THU",
            "value" : []
        },
        {
            "key":"FRI",
            "value" : []
        },
        {
            "key":"SAT",
            "value" : []
        }
    ]

Please help . Thank you

CodePudding user response:

converting your date string to a Date object allows you to get the date number of the week using getDay() which gives a number (0-6)
can create a function to return the day name based on the day number

after that it is the usual grouping using reduce

const x = {    "data": {       "fetchOrdersAndRequest": [{                "id": "PI786971",                "customerId": [                    "C200147"                ],               "createdDate": "2022-09-21T04:46:00.126Z",            },            {                "id": "PI786969",                "customerId": [                    "C200146"                ],                "createdDate": "2022-09-20T04:46:00.126Z",},            {                "id": "PI786968",                "customerId": [                    "C200145"                ],                "createdDate": "2022-09-20T04:46:00.126Z",            }        ]    }}

const getDayName = (day) => {
    return ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][day]
}

const res = x.data.fetchOrdersAndRequest.reduce((acc,curr) => {
  const {createdDate} = curr
  const day = new Date(createdDate).getDay()
  acc[getDayName(day)].push(curr)
  return acc
},{'Sun':[], 'Mon':[], 'Tue':[], 'Wed':[], 'Thu':[], 'Fri':[], 'Sat':[]})

console.log(res)

EDIT the question changed and now it can be solved using the day number alone using array indices

const x = {    "data": {       "fetchOrdersAndRequest": [{                "id": "PI786971",                "customerId": [                    "C200147"                ],               "createdDate": "2022-09-21T04:46:00.126Z",            },            {                "id": "PI786969",                "customerId": [                    "C200146"                ],                "createdDate": "2022-09-20T04:46:00.126Z",},            {                "id": "PI786968",                "customerId": [                    "C200145"                ],                "createdDate": "2022-09-20T04:46:00.126Z",            }        ]    }}


const accumulator = {
    period: 'lastWeek',
    data: [{
            "key": "SUN",
            "value": []
        },
        {
            "key": "MON",
            "value": []
        },
        {
            "key": "TUE",
            "value": [],
        },
        {
            "key": "WED",
            "value": [],
        },
        {
            "key": "THU",
            "value": []
        },
        {
            "key": "FRI",
            "value": []
        },
        {
            "key": "SAT",
            "value": []
        }
    ]
}

const res = x.data.fetchOrdersAndRequest.reduce((acc,curr) => {
  const {createdDate} = curr
  const day = new Date(createdDate).getDay()
  acc.data[day].value.push(curr)
  return acc
},accumulator)

console.log(res)

CodePudding user response:

You could loop through the objects and then use: new Date(item.createdDate).getDay() to check wich day the date is on.

Then use that to push your items to the new array.

  • Related