Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

101
Vistas
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>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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>

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you're at all concerned with time complexity you could do this in one 'n' by implementing a function that keeps track of both the highest and second highest values. Maybe something like this:


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()
  }
]


function getSecondHighest(dates){
    let highest = 0;
    let secondHighest = 0;

    for(const val of dates){
        const currentDate = val.date
        if(currentDate > highest){
            secondHighest = highest;
            highest = currentDate;
        } else if(currentDate > secondHighest){
            secondHighest = currentDate
        } else {
            continue;
        }
    }

    return secondHighest;
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda