我目前有两个包含日期的数组。我想在比较两个数组时检查是否存在至少一个值匹配。为了澄清,这些是Date对象而不是字符串。我的代码似乎总是返回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>
&#13;
答案 0 :(得分: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>
转换您的日期对象toString
,否则您可以使用getTime
然后进行比较。
答案 1 :(得分:0)
通过这种方式,您可以找到匹配的那个
Array.prototype.diff = function(arr2) {
var ret = [];
for(var i in this) {
if(arr2.indexOf( this[i] ) > -1){
ret.push( this[i] );
}
}
return ret;
};
答案 2 :(得分:0)
您可以使用Array
的{{3}}来检查至少一个符合条件的元素。
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);
答案 3 :(得分: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);
答案 4 :(得分:0)
您可以使用Array.some()
some()方法测试数组中至少有一个元素是否通过了由提供的函数实现的测试。
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)));
答案 5 :(得分: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>