Skip to content
Advertisement

Get the Second Highest Date in JavaScript/ES6

I have a problem getting the second highest date in ES6. I’m using moment.js too. Its supposed to be getting the id of 3.

const datas = [
    {
        id: 1,
        date: moment(String('Apple & Banana - 20072021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
    },
    {
        id: 2,
        date: moment(String('Apple & Oranges - 30082021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
    },
    {
        id: 3,
        date: moment(String('Lemon & Oranges - 30102021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
    },
    {
        id: 4,
        date: moment(String('Honeydew - 30112021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
    }
];

const secondLatestDate = new Date(datas.map(file => new Date(file.date)).sort().reverse()[1]);

const finalResult = datas.find(file => file.date.getTime() === secondLatestDate.getTime());

console.log(finalResult)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Advertisement

Answer

You should use custom sort function as:

datas.sort((a, b) => a.date - b.date)

There is no need to use find when you are reverseing the array and getting the index 1 from it.

Note: I deliberately change the order of the datas array

const datas = [{
    id: 1,
    date: moment(String('Apple & Banana - 20072021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
  },
  {
    id: 2,
    date: moment(String('Apple & Oranges - 30082021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
  },
  {
    id: 4,
    date: moment(String('Honeydew - 30112021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
  },
  {
    id: 3,
    date: moment(String('Lemon & Oranges - 30102021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
  }
];

const secondLatestDate = datas.sort((a, b) => a.date - b.date).reverse()[1];
console.log(secondLatestDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

or you can directly find the second largest after sort. There is no need to reverse the array

datas.sort((a, b) => a.date - b.date)[datas.length - 2]

const datas = [{
    id: 1,
    date: moment(
      String('Apple & Banana - 20072021').match(/[0-9]/g).join(''),
      'DDMMYYYY'
    ).toDate(),
  },
  {
    id: 2,
    date: moment(
      String('Apple & Oranges - 30082021').match(/[0-9]/g).join(''),
      'DDMMYYYY'
    ).toDate(),
  },
  {
    id: 4,
    date: moment(
      String('Honeydew - 30112021').match(/[0-9]/g).join(''),
      'DDMMYYYY'
    ).toDate(),
  },
  {
    id: 3,
    date: moment(
      String('Lemon & Oranges - 30102021').match(/[0-9]/g).join(''),
      'DDMMYYYY'
    ).toDate(),
  },
];

const secondLatestDate = datas.sort((a, b) => a.date - b.date)[datas.length - 2];
console.log(secondLatestDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement