Update - It appears that we might be dealing with a localized bug. Further testing is required to confirm...
I have a CSV String (containing spaces) that resembles the following:
var myString = "Here is a value, I am an important value, One last value"
I've split this String into an array using
var myArray = myString.split(", ")
. The values in myArray
are as follows:
// myArray[0] = "Here is a value"
// myArray[1] = "I am an important value"
// myArray[2] = "One last value"
Now, within a simple for loop
, I'm using an if
statement to check if "I am an important value" exists in the CSV String like so:
for (var i = 0; i < myArray.length; ++i) {
if (myArray[i] == "I am an important value") {
myBool = true;
}
}
I also tried
if (myArray[i] == 'I am an important value')
and `if (myArray[i].toString() == "I am an important value")` with no success.
The strange part is that the value does exist in the CSV String, they are separated correctly (no trailing spaces), but the condition isn't returning true for some reason. Is there a hidden factor that I'm overlooking? I purposely used ==
for type conversion purposes. Changing to ===
didn’t change anything. The loop iterates through each element in myArray
and sets myBool
to true when it finds the value, so it's perplexing why it's not working immediately.
What's even more peculiar is that I compared the character count of each myArray
element to the input String and found them identical. So why isn't the equality comparison acting as expected?
PS - An interesting point about this issue is how a similar approach earlier in the function, maintaining the same input type, works perfectly fine. If, for instance, I remove "Here is a value" from the original CSV String, then the condition evaluates to true and myBool
switches to true (as long as the comparison value is the first element). Otherwise, it fails to do so.
PPS - The individual values in the original CSV String won't contain commas, hence myString.split(", ");
suits the purpose.
UPDATE: On separating the function into its script file and running tests, an unexpected behavior surfaced (what exactly is String.split()
doing?)
/*
* This illustrates the data setup within my script. An Object holds a String property.
* The object itself is passed as a parameter, upon which I perform checks:
*/
function start() {
var myObject = {
"property": "Here is a value, I am an important value, One last value",
"extra": false,
}
var result = stringSplitTest(myObject);
Logger.log("Result: " + result);
}
function stringSplitTest(someObject) {
var myBool = false;
var stringToSplit = someObject.property;
Logger.log(stringToSplit);
var array = stringToSplit.split(", ");
for (var i = 0; i < array.length; ++i) {
Logger.log("Index " + i + ": " + array[i]);
if (array[i] == "I am an important value") {
myBool = true;
break;
}
}
return myBool;
}
Logging output:
[13-07-25 11:29:45:628 EDT] Here is a value, I am an important value, One last value
[13-07-25 11:29:45:628 EDT] Index 0: Here is a value
[13-07-25 11:29:45:628 EDT] Index 1: I am an important value
[13-07-25 11:29:45:628 EDT] Result: true