0

I currently have two arrays containing dates. I want to check if there's at least one match of values when comparing the two arrays. To clarify, these are Date objects and not strings. My code below always seems to return false:

var between = [Sun Aug 27 2017 00:00:00 GMT+0100 (BST), Mon Aug 28 2017 00:00:00 GMT+0100 (BST), Tue Aug 29 2017 00:00:00 GMT+0100 (BST), Wed Aug 30 2017 00:00:00 GMT+0100 (BST)];
var myDate = [Mon Aug 28 2017 00:00:00 GMT+0100 (BST), Thu Aug 24 2017 00:00:00 GMT+0100 (BST)];

var bExists = false;
$.each(myDate, function(index, value){
  if($.inArray(value,between)!=-1){
      console.log(value);
      bExists = true;
  }
  if(bExists){
      return false; //break
  }
});

console.log(bExists);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
dreamkiller
  • 189
  • 3
  • 17

6 Answers6

0

var between = [new Date('Sun Aug 27 2017 00:00:00 GMT+0100 (BST)'), new Date('Mon Aug 28 2017 00:00:00 GMT+0100 (BST)'), new Date('Tue Aug 29 2017 00:00:00 GMT+0100 (BST)'), new Date('Wed Aug 30 2017 00:00:00 GMT+0100 (BST)')];
var myDate = [new Date('Mon Aug 28 2017 00:00:00 GMT+0100 (BST)'), new Date('Thu Aug 24 2017 00:00:00 GMT+0100 (BST)')];

var bExists = false;
$.each(myDate, function(index, myDateValue){

    $.each(between, function(index, betweenValue){
      if(myDateValue.toString() == betweenValue.toString()){
        console.log(betweenValue);
        bExists = true;
        return false;
      }
    })

});

console.log(bExists);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Convert your Date object toString or else you can use getTime and then compare.

Durga
  • 15,263
  • 2
  • 28
  • 52
  • The issue is my arrays don't contain the dates as strings and they are varying based on user selection as part of the wider implementation. Would I have to convert each element within both arrays to strings first? – dreamkiller Aug 24 '17 at 06:12
  • @dreamkiller you mean Date object you are storing in array? – Durga Aug 24 '17 at 06:18
  • @dreamkiller ok do one thing convert your Date object to `toString()` or `getTime()` then check it. – Durga Aug 24 '17 at 07:08
0

In this way you can find the one's that match

Array.prototype.diff = function(arr2) {
var ret = [];
for(var i in this) {   
    if(arr2.indexOf( this[i] ) > -1){
        ret.push( this[i] );
    }
}
return ret;
};
Mohhamad Hasham
  • 1,750
  • 1
  • 15
  • 18
0

You can make use of .some of Array to check at least one element satisfy the condition.

var between = ["Sun Aug 27 2017 00:00:00 GMT+0100 (BST)", "Mon Aug 28 2017 00:00:00 GMT+0100 (BST)", "Tue Aug 29 2017 00:00:00 GMT+0100 (BST)", "Wed Aug 30 2017 00:00:00 GMT+0100 (BST)"];
var myDate = ["Mon Aug 28 2017 00:00:00 GMT+0100 (BST)", "Thu Aug 24 2017 00:00:00 GMT+0100 (BST)"];

//checking atleast one present
var atleastOne = myDate.some(function(item){
  return between.indexOf(item) > -1;
});

//checking that all present
var all = myDate.every(function(item){
  return between.indexOf(item) > -1;
});

console.log(all);
Jyothi Babu Araja
  • 10,076
  • 3
  • 31
  • 38
0
var between = [Sun Aug 27 2017 00:00:00 GMT+0100 (BST), Mon Aug 28 2017 00:00:00 GMT+0100 (BST), Tue Aug 29 2017 00:00:00 GMT+0100 (BST), Wed Aug 30 2017 00:00:00 GMT+0100 (BST)];
var myDate = [Mon Aug 28 2017 00:00:00 GMT+0100 (BST), Thu Aug 24 2017 00:00:00 GMT+0100 (BST)];

var bExists = false;

for ( var i = 0; i < between.length; i++ ) {
    for ( var e = 0; e < myDate.length; e++ ) {
        if ( between[i] === myDate[e] ){
            bExists = true;
    }
}

console.log(bExists);
Blue
  • 312
  • 2
  • 12
0

You could use Array.some()

The some() method tests whether at-least one element in the array passes the test implemented by the provided function.

var between = ['Sun Aug 27 2017 00:00:00 GMT+0100 (BST)', 'Mon Aug 28 2017 00:00:00 GMT+0100 (BST)','Tue Aug 29 2017 00:00:00 GMT+0100 (BST)', 'Wed Aug 30 2017 00:00:00 GMT+0100 (BST)'];
var myDate = ['Mon Aug 28 2017 00:00:00 GMT+0100 (BST)', 'Thu Aug 24 2017 00:00:00 GMT+0100 (BST)'];

 console.log(between.some(date => myDate.includes(date)));
Dinesh undefined
  • 5,490
  • 2
  • 19
  • 40
0

var between = ['Sun Aug 27 2017 00:00:00 GMT+0100 (BST)', 'Mon Aug 28 2017 00:00:00 GMT+0100 (BST)', 'Tue Aug 29 2017 00:00:00 GMT+0100 (BST)', 'Wed Aug 30 2017 00:00:00 GMT+0100 (BST)'];
var myDate = ['Mon Aug 28 2017 00:00:00 GMT+0100 (BST)', 'Thu Aug 24 2017 00:00:00 GMT+0100 (BST)'];

  var isExist = false;
    //checking atleast one present
    $.each(myDate ,function(index , value){ 
   if(between.indexOf(value) != "-1")
    {
     isExist = true; 
     return;
    }
  
    });

console.log(isExist);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
jANVI
  • 718
  • 3
  • 12