Home > front end >  How to group according to specific date
How to group according to specific date

Time:09-23

const a = [
  {
    "record_id": "2ff8212f-5ec9-4453-b1f3-91840a3fb152",
    "status_date_activity": {
      "On Rent": 1662021991000
    }
  },
  {
    "record_id": "c8c11f97-5eda-4f9b-a6a0-5a3f259c85b6",
    "status_date_activity": {
      "On Rent": 1663826452195
    }
  },
  {
    "record_id": "8c23aa44-4113-4feb-92b7-eb265f3e11e2",
    "status_date_activity": {
      "On Rent": 1663837445146,
      "Draft": 1663808712701,
      "Active": 1663808565409
    }
  },
  {
    "record_id": "fd88fbfc-a8d3-4a86-b245-0f8334b4f11f",
    "status_date_activity": {
      "On Rent": 1663841622113
    }
  },
  {
    "record_id": "e0ed3dcf-943c-48d1-b387-87e758e5ed9a",
    "status_date_activity": {
      "On Rent": 1663814717259,
      "Unprepped": 1663841617839
    }
  },
  {
    "record_id": "cef4d093-0ced-4d0d-b9f6-90c2e2687bbe",
    "status_date_activity": {
      "On Rent": 1663892940412
    }
  }
]

Given the array above, my goal is to group according to date(On Rent). The key must start on the first day of month and end up to current day. It should look like this

// Note: if the value dont have Unprepped, it will still display on the key(which is the date it is on rent up to the current date. but if it has a value on Unprepped it will stop on the date..)

for example today is september 23, 2022

const final_result = [
  {
    "Sept 1": [
      {
        //for example this is vehicle23
        record_id: "2ff8212f-5ec9-4453-b1f3-91840a3fb152",
        status_date_activity: {
          "On Rent": 1662021991000
        }
      }
    ]
  },
  {
    "Sept 2": [...and so on ] // vehicle23 will display here because there is no unprepped  in status_date_activity
  },
  ...and so on..., // vehicle23 will display here because there is no unprepped  in status_date_activity
  {
    "Sept 22":[
       {// vehicle23 will display here because there is no unprepped  in status_date_activity},

      {
        //for example this is vehicle4
        "record_id": "e0ed3dcf-943c-48d1-b387-87e758e5ed9a", 
        "status_date_activity": {
          "On Rent": 1663814717259, 
          "Unprepped": 1663922791000 
        }
      },
    ]
  },
  {
    "Sept 23":[
      {// vehicle23 will display here because there is no unprepped  in status_date_activity},
      {
        //vehicle4 will still display in sept 23 and will not display in sept24 because the value of unprepped is only for sept23
        "record_id": "e0ed3dcf-943c-48d1-b387-87e758e5ed9a",
        "status_date_activity": {
          "On Rent": 1663814717259,
          "Unprepped": 1663922791000
        }
      },
    ]
  },
  {
    "Sept 24": [...and so on ] // vehicle23 will display here because there is no unprepped  in status_date_activity

  }
];

CodePudding user response:

Here you go i hope it will your expectation

but one thing i changed in your data structure is On Rent to On_Rent if that is real we can't change the timestamp to date or else if you want only On Rent instead of On_rent mention in the comment i will send there one line of code to convert that into to original

const a = [
  {
    "record_id": "2ff8212f-5ec9-4453-b1f3-91840a3fb152",
    "status_date_activity": {
      "On Rent": 1662021991000
    }
  },
  {
    "record_id": "c8c11f97-5eda-4f9b-a6a0-5a3f259c85b6",
    "status_date_activity": {
      "On Rent": 1663826452195
    }
  },
  {
    "record_id": "8c23aa44-4113-4feb-92b7-eb265f3e11e2",
    "status_date_activity": {
      "On Rent": 1663837445146,
      "Draft": 1663808712701,
      "Active": 1663808565409
    }
  },
  {
    "record_id": "fd88fbfc-a8d3-4a86-b245-0f8334b4f11f",
    "status_date_activity": {
      "On Rent": 1663841622113
    }
  },
  {
    "record_id": "e0ed3dcf-943c-48d1-b387-87e758e5ed9a",
    "status_date_activity": {
      "On Rent": 1663814717259,
      "Unprepped": 1663841617839
    }
  },
  {
    "record_id": "cef4d093-0ced-4d0d-b9f6-90c2e2687bbe",
    "status_date_activity": {
      "On Rent": 1663892940412
    }
  }
]

const fun = (ar)=>{
const hetMap = ar.map((e, i)=>{
  var modifyItem;
   for(x in e.status_date_activity){
      modifyItem = x.split(' ').join('_')
      if(x.split(' ').length > 1){
         e.status_date_activity[modifyItem] = e.status_date_activity[x]
        delete e.status_date_activity[x]
}
       
   }
  const d = new Date(e.status_date_activity.On_Rent)
  const month = d.toLocaleString('en-US', {
    month: 'long',
  });
  const date = d.getDate()
   const getPerfectFormat = `${date} ${month}`
   const Data ={ [getPerfectFormat] :[{
     record_id : e.record_id,
     status_date_activity : e.status_date_activity
}]}
return Data
})
return hetMap
}
console.log(fun(a))

CodePudding user response:

can you try this once
Is this the way you want to get the output ?

const a = [
  {
    "record_id": "2ff8212f-5ec9-4453-b1f3-91840a3fb152",
    "status_date_activity": {
      "On Rent": 1662021991000
    }
  },
  {
    "record_id": "c8c11f97-5eda-4f9b-a6a0-5a3f259c85b6",
    "status_date_activity": {
      "On Rent": 1663826452195
    }
  },
  {
    "record_id": "8c23aa44-4113-4feb-92b7-eb265f3e11e2",
    "status_date_activity": {
      "On Rent": 1663837445146,
      "Draft": 1663808712701,
      "Active": 1663808565409
    }
  },
  {
    "record_id": "fd88fbfc-a8d3-4a86-b245-0f8334b4f11f",
    "status_date_activity": {
      "On Rent": 1663841622113
    }
  },
  {
    "record_id": "e0ed3dcf-943c-48d1-b387-87e758e5ed9a",
    "status_date_activity": {
      "On Rent": 1663814717259,
      "Unprepped": 1663841617839
    }
  },
  {
    "record_id": "cef4d093-0ced-4d0d-b9f6-90c2e2687bbe",
    "status_date_activity": {
      "On Rent": 1663892940412
    }
  }
]
const fun = (ar, obj)=>{
  const getSort = ar.sort((a, b)=> new Date(a.status_date_activity["On Rent"]) - new Date(b.status_date_activity["On Rent"]));
//   console.log(getSort)
const hetMap = getSort.map((e, i)=>{
  var modifyItem;
   for(x in e.status_date_activity){
      modifyItem = x.split(' ').join('_')
      if(x.split(' ').length > 1){
         e.status_date_activity[modifyItem] = e.status_date_activity[x]
        delete e.status_date_activity[x]
}
       
   }
  const d = new Date(e.status_date_activity.On_Rent)
  const month = d.toLocaleString('en-US', {
    month: 'long',
  });
  const date = d.getDate()
   const getPerfectFormat = `${date} ${month}`
   const Data ={ [getPerfectFormat] :[{
     record_id : e.record_id,
     status_date_activity : e.status_date_activity
}]}
return Data
})

  hetMap.forEach((e, i)=>{
    for(x in e){
     obj[x] = e[x]
}
})
return obj
}
console.log(fun(a ,{}))

  • Related