Skip to content
Advertisement

How to group according to date{ the key in each object is the day – from the first day of the month untill the current date}

const a = [
    {
      "record_id": "2ff8212f-5ec9-4453-b1f3-91840a3fb152",
      "status_date_activity": {
        "On Rent": 1663841740869
      }
    },
    {
      "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 return an array of object that are group according to date(On Rent). But first I need to get the start of the month (for example this month is september, and also the current date of this month so for example Sept 25) so my final output should be like this

const final_result = [
{
"Sept 1": [] // empty array because theres is no on rent on this day
},
{
"Sept 2": []// empty array because theres is no on rent on this day
},
...and so on on,
{
"Sept 22":[{
...the value above that on rent is belong to this date( no matter what time it is)
}]
},
...until it reaches to 
"Sept 25":[]// empty array because theres is no on rent on this day
]

Also I made this code but I need to get the firstDateOfTheMonth and the currentDate

const b = a.reduce((acc,curr)=>{
  const dateN = new Date(curr.status_date_activity?.['On Rent']).toLocaleDateString("en-us", {
    day: "numeric",
    month: "short"
  })
  return {
    ...acc,
    [dateN]:[...(acc[dateN] ?? []),curr]
  }
},{})

Advertisement

Answer

Grouping approach

  1. Make an empty object days with key as date like
{
  "Sep 1": {
    "Sep 1": []
  },
  "Sep 2": {
    "Sep 2": []
  },
  1. Merge empty object with an array

  2. Get only values of merged object

Live Demo

const arr = [{"record_id": "2ff8212f-5ec9-4453-b1f3-91840a3fb152","status_date_activity": {"On Rent": 1663841740869}},{"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 getRentValue = (item) => item["status_date_activity"]["On Rent"];
const getShortDate = (date) => date.toLocaleDateString("en-US", { month: "short", day: 'numeric' });

const anyDate = new Date(getRentValue(arr.at(0)));
const year = anyDate.getFullYear();
const month = anyDate.getMonth();
const lastMonthDay = new Date(year, month + 1, 0).getDate();

const days = Array(lastMonthDay).fill()
  .reduce((acc, _, i) => {
    const shortDate = getShortDate(new Date(year, month, i + 1));
    acc[shortDate] = { [shortDate]: [] };
    return acc;
  }, {});

const merged = arr.reduce((acc, item) => {
  const shortDate = getShortDate(new Date(getRentValue(item)));
  acc[shortDate][shortDate].push(item);
  return acc;
}, days);

const result = Object.values(merged);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement