Tengo un problema para obtener la segunda fecha más alta en ES6. Estoy usando moment.js también. Se supone que debe obtener la id de 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>Debe usar la función de clasificación personalizada como:
datas.sort((a, b) => a.date - b.date) No hay necesidad de usar find cuando está reverse la matriz y obteniendo el índice 1 de ella.
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> o puede encontrar directamente el segundo más grande después de ordenar. No hay necesidad de reverse la matriz
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>Si está preocupado por la complejidad del tiempo, puede hacer esto en uno 'n' implementando una función que realiza un seguimiento de los valores más altos y segundos más altos. Tal vez algo como esto:
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; }