Reputation: 4496
I'm trying to check whether an array, called ruleFolderNames
, contains a string, using the .includes()
Javascript method. The string I am checking is "Centers For Medicare & Medicaid Services"
.
ruleFolderNames.includes("Centers For Medicare & Medicaid Services"); // true
However, I'm not typing the string directly, I'm using a variable (called department
). I've found that the department
variable is not equal to the first item in the array, Centers For Medicare & Medicaid Services
, even though they appear to be the same.
Here are the console.log
statements:
ruleFolderNames[0]; // Centers For Medicare & Medicaid Services
typeof ruleFolderNames[0]; // string
department; // Centers For Medicare & Medicaid Services
typeof department; // string
But in fact, the variable department
is not equal to the string I'm searching for.
department == "Centers For Medicare & Medicaid Services" // OUTPUTS FALSE!!!
I've included the link to my full Github repository if that's useful, the bug is contained on line 36 of this file.
Upvotes: 0
Views: 34
Reputation: 707228
Since we don't have the actual data ourselves to debug, we can't tell you exactly what difference there is. Here are a number of things to look for:
Check the length of both strings to see if there might be a difference in leading or trailing whitespace that isn't obvious in the console. You can also output in the console with quotes surrounding it to see exactly what might be on the start or end such as console.log(`'${department}'`)
and ruleFolderNames.forEach(item => console.log(`'${item}'`))
.
White space characters might be different, but look like regular whitespace in the console. If you suspect the difference is in the department variable (which it appears it is), then you can normalize the whitespace with department.replace(/\s/g, " ")
which converts all regex whitespace characters to a single space.
There could be some character that looks similar, but is actually a different char code.
Since you say that department == "Centers For Medicare & Medicaid Services"
is false, I'd suggest the following debug code (with your department
variable passed to testStr()
:
function testStr(testVal, targetStr) {
console.log("-------------");
if (testVal === targetStr) {
console.log("two strings are the same");
return;
}
if (testVal.length !== targetStr.length) {
console.log("two strings are different length");
if (testVal.trim() === targetStr) {
console.log("Found it: testVal.trim() === targetStr");
return;
} else {
console.log("testVal.trim() !== targetStr, trimming whitespace off ends is not enough");
}
testVal = testVal.trim();
}
if (testVal.replace(/\s/g, " ") === targetStr) {
console.log("replacing whitespace makes a match")
return;
}
// still no match, so lets output each separate character as a char code
// to manually see what's different
console.log("testVal = ", testVal.split("").map(item => item.charCodeAt(0)).join(","));
console.log("targetStr = ", targetStr.split("").map(item => item.charCodeAt(0)).join(","));
}
const testValues = [
" Centers For Medicare & Medicaid Services", // leading whitespace
"Centers For Medicare & Medicaid Services ", // trailing whitespace
"Centers\tFor Medicare & Medicaid Services", // tab for whitespace
"C℮nters For Medicare & Medicaid Services", // odd first e in Centers
"Cënters For Medicare & Medicaid Services" // odd first e in Centers
];
for (let t of testValues) {
testStr(t, "Centers For Medicare & Medicaid Services");
}
Upvotes: 1