Next.js Discord

Discord Forum

How to map based on date?

Unanswered
Asian black bear posted this in #help-forum
Open in Discord
Avatar
Asian black bearOP
I wanna map init based on date where the most recent one get inserted first, is it possible to use init.map()?
const init =[{
    id: 'c2600cde-3cc2-4d2d-a08e-fa75ed0e04bf',
    amount: 666666,
    date: 2022-06-08T22:00:00.000Z,
    name: 'Name3'
  },
{
    id: 'jre234de-3cc2-4d2d-a08e-fa75ed0e04bf',
    amount: 444,
    date: 2024-07-08T22:00:00.000Z,
    name: 'Name2'
  }
,{
    id: 'c2546cde-3cc2-4d2d-a08e-fa75ed0e04bf',
    amount: 123,
    date: 2023-06-08T22:00:00.000Z,
    name: 'Name1'
  }]

6 Replies

Avatar
Asian black bearOP
upp
Avatar
Asian black bearOP
js has its own sort but I wonder if next has a more robust feature?
Avatar
Asian black bearOP
anyone ?
Avatar
no, what you're looking for is init.sort with a custom sort function (a.k.a comparator)
const sortedInit 
  = [...init].sort((a, b) => new Date(b.date) - new Date(a.date));
also please only bump once a day