简体   繁体   English

Javascript-从另一个Date()对象数组中删除Date()对象数组

[英]Javascript - Remove Array of Date() objects from another array of Date() objects

I have two arrays, Array1 has roughly 500 Date() objects in it. 我有两个数组,Array1中大约有500个Date()对象。 Array2 has about 200 Date() objects. Array2有大约200个Date()对象。 All of these dates are separate instances but Array2 Date() objects will have a matching Date() in Array1. 所有这些日期都是单独的实例,但是Array2 Date()对象在Array1中将具有匹配的Date()。

I need to remove the contents of Array2 from the contents of Array1. 我需要从Array1的内容中删除Array2的内容。

I have considered creating a separate array of unix timestamps from array1 for comparison, but I was hoping there was an easier/more efficient way of doing this. 我考虑过从array1创建一个单独的unix时间戳数组进行比较,但是我希望有一种更容易/更有效的方法。

Try a simple filter like this: 尝试这样的简单过滤器:

var values = Array2.map(function(date) { return date.getTime(); });
var unique = Array1.filter(function(date) { return values.indexof(date.getTime()) == -1 });

If a date in Array1 is also in Array2 (if indexof is not -1) then it will be filtered out. 如果Array1中的日期也位于Array2中(如果indexof不为-1),则它将被滤除。 When you are only working with arrays of 200 - 500 dates at a time, efficiency should not be a concern. 当您一次仅处理200-500个日期的数组时,效率不成问题。

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

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