I am attempting to see if the current list of selected values are included in 2 or more array that I have setup;
My Check gets Completely Bypassed and Never Equates to True
tempDaysArray = ["13", "14", "15" ,"16" , "17" , "18" , "19" , "20", "21", "22","23","24","25","26"]; // just an example but its more or less how the values would look
var xmasPhase1: string[];
var xmasPhase2: string[];
var xmasPhase3: string[];
xmasPhase1 = ["15" , "16" , "17" , "18" , "19" , "20"];
xmasPhase2 = ["21" , "22" , "23" , "24" , "25" , "26"];
xmasPhase3 = ["27" , "28" , "29" , "30" , "31"];
const multiPhase = tempDaysArray.some(r=> xmasPhase1.includes(r) && xmasPhase2.includes(r) || xmasPhase3.includes(r) && xmasPhase2.includes(r) || xmasPhase1.includes(r) && xmasPhase3.includes(r))
if(multiPhase){
//Do Something
}
I know thats an extremely Ugly approach but its the closest ive come to something concrete and im clean out of ideas
Im aware of a few ways to Compare an Array to an Array The issue is finding similarity in 2 arrays. having X have a Similarity in Y & Z to do something
CodePudding user response:
Based on additional information from the comments, I am assuming you want to know if values from the tempDaysArray intersect (fall into) with more than just one of the three xmasPhase arrays.
Then you would change the logic, to check which of the xmasPhase arrays include at least one of the values from the tempDaysArray. So you would iterate over all xmasPhase arrays and do the check.
After you do that, then a multiPhase is just a result that is longer than 1.
Feel free to correct me in the comments if my assumptions are wrong, and I can try to adjust the logic.
// just an example but its more or less how the values would look
tempDaysArray = ["13", "14", "15" ,"16" , "17" , "18" , "19" , "20", "21", "22","23","24","25","26"];
xmasPhase1 = ["15" , "16" , "17" , "18" , "19" , "20"];
xmasPhase2 = ["21" , "22" , "23" , "24" , "25" , "26"];
xmasPhase3 = ["27" , "28" , "29" , "30" , "31"];
activePhases = [xmasPhase1, xmasPhase2, xmasPhase3].filter(phase =>
phase.some(day => tempDaysArray.includes(day))
)
multiPhase = activePhases.length > 1
if(multiPhase){
//Do Something
console.log('tempDaysArray is a multi phase')
}
CodePudding user response:
I would Very much Appreciate an Improvement on this solution if possible, Its Extremely Scruffy but this resolves the question for me:
const multiPhase3 = tempDaysArray.some(r=> xmasPhase1.includes(r));
const MultiPhase2 = tempDaysArray.some(r=> xmasPhase2.includes(r));
const multiPhase = tempDaysArray.every(r=> xmasPhase3.includes(r));
if(multiPhase && MultiPhase2 || MultiPhase2 && multiPhase3 || multiPhase && multiPhase3){
this.errorType = 'decMultiPhaseError';
this.banperiod = true;
return false;
}
