简体   繁体   English

获取最近的上一个日期,在不修改原始数组的情况下进行排序

[英]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我正在尝试以这种方式获取最接近formatted_date =2020-08-04 的日期,其中最接近的是前一个日期--> 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?我需要arrayDateValue来进一步处理,如何在不修改的情况下对其进行排序?

  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?我需要arrayDateValue来进一步处理,如何在不修改的情况下对其进行排序?

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.可以使用通过arrayDateValue数组的单次迭代来完成相同的操作。 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.一种简单的方法是编写一个解析并比较 function ,它将接受日期参数并将其拆分为yearmonthday变量,然后可以用于与我们的result变量进行比较,该变量将在开始时初始化并在每次迭代使用上述 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);

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

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