2016-09-30 67 views
0

我有两个日期数组。第一个具有所有预订日期,第二个具有用户将选择的“开始日期”和“结束日期”之间的日期。如何检查两个日期数组是否有匹配的项目?

现在我必须确认从完全预订的日期数组中找不到开始和停止之间的天数。

我正在使用Vue.js来更新数据。

这里是我做了什么让这些日期:

  /** 
      * Get dates from datepicker and format it to the same format that fully booked array has it's dates. 
      **/     
      var day1 = moment(this.startDate, 'DD/MM/Y').format('Y,M,D'); 
      var day2 = moment(this.endDate, 'DD/MM/Y').format('Y,M,D'); 
      var start = new Date(day1); 
      var end = new Date(day2); 

      /** 
      * Get dates between start and stop and return them in the dateArray. 
      **/ 
      Date.prototype.addDays = function(days) { 
       var dat = new Date(this.valueOf()); 
       dat.setDate(dat.getDate() + days); 
       return dat; 
      }; 

      function getDates(startDate, stopDate) { 
       var dateArray = []; 
       var currentDate = startDate; 
       while (currentDate <= stopDate) { 
        dateArray.push(currentDate); 
        currentDate = currentDate.addDays(1); 
       } 
       return dateArray; 
      } 

      var dateArray = getDates(start, end); 

      /** 
      * Set dateArray in to Vue.js data. 
      **/ 
      this.$set('daysBetweenStartStop', dateArray); 

      /** 
      * Get arrays of dates from the Vue.js data. calendar = Booked dates | matchingDays = Dates between start and stop. 
      **/ 
      var calendar = this.fullDates; 
      var matchingDays = this.daysBetweenStartStop; 

      /** 
      * @description determine if an array contains one or more items from another array. 
      * @param {array} haystack the array to search. 
      * @param {array} arr the array providing items to check for in the haystack. 
      * @return {boolean} true|false if haystack contains at least one item from arr. 
      */ 
      var findIfMatch = function (haystack, arr) { 
       return arr.some(function (v) { 
        return haystack.indexOf(v) >= 0; 
       }); 
      }; 
      var matching = findIfMatch(calendar, matchingDays); 

      /** 
      * Check the results. If false we are good to go. 
      **/ 
      if (matching){ 
       alert('WE FOUND A MATCH'); 
      } else { 
       alert('GOOD TO GO'); 
      } 

数组是按以下格式:

var calendar = [ 
Sun Oct 02 2016 00:00:00 GMT+0300 (EEST), 
Sun Oct 09 2016 00:00:00 GMT+0300 (EEST), 
Sun Oct 16 2016 00:00:00 GMT+0300 (EEST), 
Sun Oct 23 2016 00:00:00 GMT+0300 (EEST), 
Sun Oct 30 2016 00:00:00 GMT+0300 (EEST) 
] 

var matchingDays = [ 
Fri Oct 28 2016 00:00:00 GMT+0300 (EEST), 
Sat Oct 29 2016 00:00:00 GMT+0300 (EEST), 
Sun Oct 30 2016 00:00:00 GMT+0300 (EEST) 
] 

我的问题是,即使这两个数组具有完全相同的日期,但它们仍然会以某种方式被视为不相同。任何想法如何得到这个工作?

+0

是数组总是排序吗? – Gal

+0

你的意思是你需要看到两个数组之间的区别还是需要看到数组中的匹配项? –

+0

@Gal是的,完全预订的数组来自其他函数,它过滤来自另一个数组的特定日子,然后将它们设置为完全预订的数组。匹配日期数组来自开始日期和结束日期,这将从开始到结束将项目推送到数组。 – Eljas

回答

1

你匹配功能应该是这样的:

findIfMatch = function (haystack, arr){ 
     var i = 0;//array index 
     var j = 0;//array index 
     while(j < arr.length && i < haystack.length){ 

      cur_cal = Date.parse(haystack[i]); 
      cur_match = Date.parse(arr[j]); 
      if(cur_cal > cur_match){ 
       j++; 
      }else if(cur_cal < cur_match){ 
       i++; 
      }else{ 
       return true; 
      } 
     } 
     return false; 
} 
+0

非常好,我喜欢这种方法,因为它不构建新的数组。 – Eljas

1

从两个阵列获得匹配的记录使用此

var calendar = [ 
Sun Oct 02 2016 00:00:00 GMT+0300 (EEST), 
Sun Oct 09 2016 00:00:00 GMT+0300 (EEST), 
Sun Oct 16 2016 00:00:00 GMT+0300 (EEST), 
Sun Oct 23 2016 00:00:00 GMT+0300 (EEST), 
Sun Oct 30 2016 00:00:00 GMT+0300 (EEST) 
]; 
var matchingDays = [ 
Fri Oct 28 2016 00:00:00 GMT+0300 (EEST), 
Sat Oct 29 2016 00:00:00 GMT+0300 (EEST), 
Sun Oct 30 2016 00:00:00 GMT+0300 (EEST) 
]; 
var newCalendar = []; 
var newMatchingDays = [] 
$.map(calendar, function(date){ 
    newCalendar.push(date.toString()) 
}); 

$.map(matchingDays, function(date){ 
    newMatchingDays.push(date.toString()) 
}); 

var result = []; 
$.map(newCalendar, function (val, i) { 
    if ($.inArray(val, newMatchingDays) > -1) { 
     result.push(val); 
    } 
}); 
console.log(result); 
+0

啊,完美。所以基本上我现在需要检查的是result.length === 0,为我的任务得到正确的真假。 – Eljas

+0

是的你正确 –

+0

如果你不介意使用第三方库,看看下划线intersectionWith,我会更新我的答案来显示示例。 – Keith

1

首先你可以不喜欢这样比较日期,日期是一个对象。 例如。

var d1 = new Date('2016-09-30'); 
var d1 = new Date('2016-09-30'); 
console.log(d1 === d2); // = false 

您需要循环数组并比较每个项目,而不是使用indexOf。 或者可能使用Array.filter()。或者可以使用和对象作为查找。

例如。如果说你有 - >

var calendar = { 
    "Sun Oct 02 2016 00:00:00 GMT+0300 (EEST)": true, 
    "Sun Oct 09 2016 00:00:00 GMT+0300 (EEST)": true, 
    "Sun Oct 16 2016 00:00:00 GMT+0300 (EEST)": true, 
    "Sun Oct 23 2016 00:00:00 GMT+0300 (EEST)": true, 
    "Sun Oct 30 2016 00:00:00 GMT+0300 (EEST)": true 
}; 
if (calendar["Sun Oct 16 2016 00:00:00 GMT+0300 (EEST)"]) { 
    console.log("We have date"); 
} 

请注意我如何使用字符串表示的日期,这也可能是一个数字,例如。来自Date.getTime()。

这里是使用Array.filter,我不认为它是一个对象查找的快速。但是,我们走了。

var calendar = [ 
    new Date('2016-09-01'), 
    new Date('2016-09-12'), 
    new Date('2016-09-10') 
]; 

var finddate = new Date('2016-09-12'); 
var found = calendar.filter(function (a) { return a.getTime() === finddate.getTime();}); 

如果你不介意使用第三方库,尽量突出..

如。

var days = [new Date('2016-09-01'), new Date('2016-09-10'), new Date('2016-09-30')]; 
var finddays = [new Date('2016-09-01'), new Date('2016-09-30')]; 

var found = _.intersectionWith(days, finddays, 
    function (a,b) { return a.getTime() === b.getTime(); }); 
相关问题