Home > database >  Group array of object by month and year
Group array of object by month and year

Time:08-24

I have an array that looks like this

myArray = [
  Object {
    "_id": "0c35e243",
    "createdDate": "2021-05-19T08:58:14.497Z",
    "description": "desc text",
  },
  Object {
    "_id": "08e05fcb",
    "createdDate": "2022-07-19T08:42:34.702Z",
    "desc": "desc text2",
  },
  Object {
    "_id": "6a33dca3",
    "createdDate": "2021-07-19T08:35:53.199Z",
    "desc": "desc text3",
  },
  Object {
    "_id": "58ea808d",
    "createdDate": "2022-08-19T08:13:35.516Z",
    "desc": "desc text4",
  },
  Object {
    "_id": "a95b706c",
    "createdDate": "2022-08-19T04:12:44.941Z",
    "desc": "desc text5",
  },
]

I want to sort it by year and month so it would look like this

newArray = [
  "2022" : [
     "7" : [
       Object {
         "_id": "08e05fcb",
         "createdDate": "2022-07-19T08:42:34.702Z",
         "desc": "desc text2",
       }
     ],
     "8" : [
       Object {
         "_id": "58ea808d",
         "createdDate": "2022-08-19T08:13:35.516Z",
         "desc": "desc text4",
       },
       Object {
         "_id": "a95b706c",
         "createdDate": "2022-08-19T04:12:44.941Z",
         "desc": "desc text5",
       },
     ]
  ],
  "2021" : [
     "5" : [
       Object {
         "_id": "0c35e243",
         "createdDate": "2021-05-19T08:58:14.497Z",
         "description": "desc text",
       }
     ],
     "7" : [
       Object {
         "_id": "6a33dca3",
         "createdDate": "2021-07-19T08:35:53.199Z",
         "desc": "desc text3",
       }
     ]
  ]
]

i've been trying to use lodash groupBy

const groupByMonth = groupBy(myArray, ({_createdDate})=> new Date(_createdDate).getMonth());

with result like this

Object {
  "5": Array [
    Object {
      "_id": "0c35e243",
      "createdDate": "2021-05-19T08:58:14.497Z",
      "description": "desc text",
    },
  ],
  "7": Array [
    Object {
      "_id": "08e05fcb",
      "createdDate": "2022-07-19T08:42:34.702Z",
      "desc": "desc text2",
    },
    Object {
      "_id": "6a33dca3",
      "createdDate": "2021-07-19T08:35:53.199Z",
      "desc": "desc text3",
    },
  ],
  "8": Array [
    Object {
      "_id": "58ea808d",
      "createdDate": "2022-08-19T08:13:35.516Z",
      "description": "desc text4",
    },
    Object {
      "_id": "a95b706c",
      "createdDate": "2022-08-19T04:12:44.941Z",
      "desc": "desc text5",
    },
  ],
}

Now what should i do next? I've tried

const groupByYear = groupBy(groupByMonth, ({_createdDate}) => new Date(_createdDate).getYear());

but i think it's not the solution since it return NaN as the year and it wont group the month like what I expected too

It's okay if the solution is not using lodash as long as the result is look like the expected result also if its possible I would like to use the month name so its not 5,7,8 but May, July, August etc.

CodePudding user response:

myArray = [
   {
    "_id": "0c35e243",
    "createdDate": "2021-05-19T08:58:14.497Z",
    "description": "desc text",
  },
  {
    "_id": "08e05fcb",
    "createdDate": "2022-07-19T08:42:34.702Z",
    "desc": "desc text2",
  },
   {
    "_id": "6a33dca3",
    "createdDate": "2021-07-19T08:35:53.199Z",
    "desc": "desc text3",
  },
   {
    "_id": "58ea808d",
    "createdDate": "2022-08-19T08:13:35.516Z",
    "desc": "desc text4",
  },
   {
    "_id": "a95b706c",
    "createdDate": "2022-08-19T04:12:44.941Z",
    "desc": "desc text5",
  },
]

const newArray = _.groupBy(myArray, ({ createdDate }) => (new Date(createdDate)).toLocaleString('en-us',{month:'short', year:'numeric'}))

console.log(newArray)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

I don't know why you append "_" to "createdDate" tho. It should work this way.

CodePudding user response:

let out = {} ;

myArray.forEach( e => {
    const [ year, month ] = e.createdDate.split("-") ;
    out[ year ] ??= {} ;
    out[ year ][ month ] ??= [] ;
    out[ year ][ month ].push( e ) ;
} ) ;

=>

{
  '2021': {
    '05': [
      {
        _id: '0c35e243',
        createdDate: '2021-05-19T08:58:14.497Z',
        description: 'desc text'
      }
    ],
    '07': [
      {
        _id: '6a33dca3',
        createdDate: '2021-07-19T08:35:53.199Z',
        desc: 'desc text3'
      }
    ]
  },
  '2022': {
    '07': [
      {
        _id: '08e05fcb',
        createdDate: '2022-07-19T08:42:34.702Z',
        desc: 'desc text2'
      }
    ],
    '08': [
      {
        _id: '58ea808d',
        createdDate: '2022-08-19T08:13:35.516Z',
        desc: 'desc text4'
      },
      {
        _id: 'a95b706c',
        createdDate: '2022-08-19T04:12:44.941Z',
        desc: 'desc text5'
      }
    ]
  }
}
  • Related