简体   繁体   中英

Getting nearest previous date, sort without modifying original array

I'm trying to get the nearest date to formatted_date =2020-08-04 in this way, where the nearest is the previous one --> 2020-08-02

 var arrayDateValue= []; var formattedDate = '2020-08-04'; arrayDateValue.push({ date: '2020-08-01', value: 1111 }); arrayDateValue.push({ date: '2020-08-02', value: 1212 }); arrayDateValue.push({ date: '2020-08-05', value: 1313 }); arrayDateValue.push({ date: '2020-08-06', value: 1313 }); var prevIndexIfNotFound = arrayDateValue.sort((a, b) => (new Date(b.date) - new Date(a.date))).findIndex(date => new Date(date.date) - new Date(formattedDate) <= 0); console.log(arrayDateValue[prevIndexIfNotFound]);

The result is perfect, but -

  1. I need arrayDateValue to further process, how to sort it without modifying it?

  2. The processing time is a little bit slower when my array is big, is there efficient way to optimize such requests?

Thanks a lot!

  1. I need arrayDateValue to further process, how to sort it without modifying it?

You could avoid mutating the original array by just using the spread operator syntax ( ... ) to create a copy of the array and then sort it so that the original array stays the same. It can be done just by adding a single line of code as -

 var arrayDateValue= []; var formattedDate = '2020-08-04'; arrayDateValue.push({ date: '2020-08-01', value: 1111 }); arrayDateValue.push({ date: '2020-08-02', value: 1212 }); arrayDateValue.push({ date: '2020-08-05', value: 1313 }); arrayDateValue.push({ date: '2020-08-06', value: 1313 }); // to avoid mutating original array var copiedArr = [...arrayDateValue] var prevIndexIfNotFound = copiedArr.sort((a, b) => (new Date(b.date) - new Date(a.date))).findIndex(date => new Date(date.date) - new Date(formattedDate) <= 0); console.log(copiedArr[prevIndexIfNotFound]);

You can read more about spread syntax if you don't know about it here


  1. The processing time is a little bit slower when my array is big, is there an efficient way to optimize such requests?

The same can be done using a single iteration through the arrayDateValue array. A simple way would be to write a parse and compare function which would take in date parameter and split it into a year , month and day variable which could then be used to compare against our result variable which would be initialized at the start and modified at each iteration to store the closest date using the above-mentioned function.

I suggest you try to code the above logic and revert back if you find any difficulty in doing so.

So loop over all the dates and check to see if the difference and keep track. No need to sort.

 var arrayDateValue = []; var formattedDate = '2020-08-04'; arrayDateValue.push({ date: '2020-08-01', value: 1111 }); arrayDateValue.push({ date: '2020-08-02', value: 1212 }); arrayDateValue.push({ date: '2020-08-05', value: 1313 }); arrayDateValue.push({ date: '2020-08-06', value: 1313 }); const closest = arrayDateValue.reduce( (obj, item) => { const diff = new Date(item.date) - new Date(formattedDate); if (diff < 0 && (.obj || obj,diff < diff)) { return { item, diff } } return obj }. null) if (closest) console.log(closest;item);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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