在多维数组中搜索 javascript
Search in the multi dimension array javascript
我遇到了 java-script 多维数组的问题。如果存在,我想在给定数组中搜索日期和月份。我的意思是我想计算除 saturday 和 sunday 和 [=26 之外的日期=]国定假日 我有这个数组
var natDays = [
[1, 1, 'uk'],
[1, 19, 'uk'],
[2, 16, 'uk'],
[4, 23, 'uk'],
[7, 03, 'uk'],
[7, 04, 'uk'],
[9, 07, 'uk'],
[8, 12, 'uk'],
[11, 11, 'uk'],
[11, 26, 'uk'],
[12, 25, 'uk']
];
参考此答案Here。我创建了这样的函数
function check_hol(month,date)
{
var natDays = [
[1, 1, 'uk'],
[1, 19, 'uk'],
[2, 16, 'uk'],
[4, 23, 'uk'],
[7, 03, 'uk'],
[7, 04, 'uk'],
[9, 07, 'uk'],
[8, 12, 'uk'],
[11, 11, 'uk'],
[11, 26, 'uk'],
[12, 25, 'uk']
];
for( var md = 0; md <= natDays.length; md++ )
{
alert(natDays[md][0]+'='+month+' and '+natDays[md][1]+'='+date) ;
if( natDays[md][0] != month && natDays[md][1] != date )
{
return true;
}
}
return false;
}
我正在使用这个函数来计算这个函数中的工作日
function workingDays(){
var n=0;
for(var i=1; i<=dp.datediff; i++){
var d1 = new Date();
d1.setDate(d1.getDate()+i);
if(d1.getDay() !== 0&&d1.getDay()!==6 && check_hol(d1.getMonth()+1,d1.getDate())===true)
{
n++;
}
}
alert(n);
dp.wdiff = n;
getShipDays();
getProdDays();
getQuantity();
getShipProdDays();
}
但它 returns 0 在工作日的输出中。如果我从我的第二个函数中删除 && check_hol(d1.getMonth()+1,d1.getDate())===true
它工作正常。
不明白我哪里错了。
而不是 !=
代码应该使用 ==
因为你想在找到值时匹配。如果您这样做,它将在整个数组中搜索值。
而不是 for
中的 <=
应该是 <
因为你的索引从 0 开始。
for (var md = 0; md < natDays.length; md++) { // changed in md < natDays
if (natDays[md][0] == month && natDays[md][1] == date) { // if values are == that means the value is found
return true; // isHoliday
}
}
function check_hol(month, date) {
var natDays = [
[1, 1, 'uk'],
[1, 19, 'uk'],
[2, 16, 'uk'],
[4, 23, 'uk'],
[7, 03, 'uk'],
[7, 04, 'uk'],
[9, 07, 'uk'],
[8, 12, 'uk'],
[11, 11, 'uk'],
[11, 26, 'uk'],
[12, 25, 'uk']
];
for (var md = 0; md < natDays.length; md++) { // changed in md < natDays
if (natDays[md][0] == month && natDays[md][1] == date) { // if values are == that means the value is found
return true; // isHoliday
}
}
return false; // !isHoliday => isWorkingDay
}
alert("found: " + check_hol(11,11));
alert("found: " + check_hol(11,12));
您需要在工作日进行一些更改,因为如果找到该值 (true
) 则意味着是 假期。
if(d1.getDay() !== 0&&d1.getDay()!==6 && !check_hol(d1.getMonth()+1,d1.getDate()))
我遇到了 java-script 多维数组的问题。如果存在,我想在给定数组中搜索日期和月份。我的意思是我想计算除 saturday 和 sunday 和 [=26 之外的日期=]国定假日 我有这个数组
var natDays = [
[1, 1, 'uk'],
[1, 19, 'uk'],
[2, 16, 'uk'],
[4, 23, 'uk'],
[7, 03, 'uk'],
[7, 04, 'uk'],
[9, 07, 'uk'],
[8, 12, 'uk'],
[11, 11, 'uk'],
[11, 26, 'uk'],
[12, 25, 'uk']
];
参考此答案Here。我创建了这样的函数
function check_hol(month,date)
{
var natDays = [
[1, 1, 'uk'],
[1, 19, 'uk'],
[2, 16, 'uk'],
[4, 23, 'uk'],
[7, 03, 'uk'],
[7, 04, 'uk'],
[9, 07, 'uk'],
[8, 12, 'uk'],
[11, 11, 'uk'],
[11, 26, 'uk'],
[12, 25, 'uk']
];
for( var md = 0; md <= natDays.length; md++ )
{
alert(natDays[md][0]+'='+month+' and '+natDays[md][1]+'='+date) ;
if( natDays[md][0] != month && natDays[md][1] != date )
{
return true;
}
}
return false;
}
我正在使用这个函数来计算这个函数中的工作日
function workingDays(){
var n=0;
for(var i=1; i<=dp.datediff; i++){
var d1 = new Date();
d1.setDate(d1.getDate()+i);
if(d1.getDay() !== 0&&d1.getDay()!==6 && check_hol(d1.getMonth()+1,d1.getDate())===true)
{
n++;
}
}
alert(n);
dp.wdiff = n;
getShipDays();
getProdDays();
getQuantity();
getShipProdDays();
}
但它 returns 0 在工作日的输出中。如果我从我的第二个函数中删除 && check_hol(d1.getMonth()+1,d1.getDate())===true
它工作正常。
不明白我哪里错了。
而不是 !=
代码应该使用 ==
因为你想在找到值时匹配。如果您这样做,它将在整个数组中搜索值。
而不是 for
中的 <=
应该是 <
因为你的索引从 0 开始。
for (var md = 0; md < natDays.length; md++) { // changed in md < natDays
if (natDays[md][0] == month && natDays[md][1] == date) { // if values are == that means the value is found
return true; // isHoliday
}
}
function check_hol(month, date) {
var natDays = [
[1, 1, 'uk'],
[1, 19, 'uk'],
[2, 16, 'uk'],
[4, 23, 'uk'],
[7, 03, 'uk'],
[7, 04, 'uk'],
[9, 07, 'uk'],
[8, 12, 'uk'],
[11, 11, 'uk'],
[11, 26, 'uk'],
[12, 25, 'uk']
];
for (var md = 0; md < natDays.length; md++) { // changed in md < natDays
if (natDays[md][0] == month && natDays[md][1] == date) { // if values are == that means the value is found
return true; // isHoliday
}
}
return false; // !isHoliday => isWorkingDay
}
alert("found: " + check_hol(11,11));
alert("found: " + check_hol(11,12));
您需要在工作日进行一些更改,因为如果找到该值 (true
) 则意味着是 假期。
if(d1.getDay() !== 0&&d1.getDay()!==6 && !check_hol(d1.getMonth()+1,d1.getDate()))