简体   繁体   English

在 JavaScript/ES6 中获取第二大日期

[英]Get the Second Highest Date in JavaScript/ES6

I have a problem getting the second highest date in ES6.我在 ES6 中获得第二高的日期时遇到问题。 I'm using moment.js too.我也在使用moment.js Its supposed to be getting the id of 3.它应该得到 3 的id

 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>

You should use custom sort function as:您应该使用自定义排序 function 作为:

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

There is no need to use find when you are reverse ing the array and getting the index 1 from it.当您reverse数组并从中获取索引1时,无需使用find

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无需reverse数组

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>

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.如果您完全关心时间复杂度,您可以通过实现一个跟踪最高值和第二高值的 function 在一个“n”中做到这一点。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM