Update - It seems that this may be a localized bug. Will do further testing to verify...
I have a CSV String (with spaces) that looks something like this:
var myString = "Here is a value, I am an important value, One last value"
I split that String into a String array using var myArray = myString.split(", ")
. So the values in myArray
are the following:
// myArray[0] = "Here is a value"
// myArray[1] = "I am an important value"
// myArray[2] = "One last value"
Now, I am using a simple if
statement inside a for loop
to evaluate if "I am an important value" was in that CSV String just like this:
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')
as well as `if (myArray[i].toString() == "I am an important value")
The bizarre thing is that the value is contained in the CSV String, they are being separated correctly (no trailing spaces and such), but the conditional is not returning true for some reason. Does anyone know if there is some hidden thing I'm missing? I intentionally use ==
so that it does the type conversion. ===
made no difference in the result. It's iterating through every element in myArray
and marks myBool
as true as soon as it sees the value, so I'm having a really hard time understanding why it's not immediately working.
The even more bizarre thing is that I counted the characters of each element in myArray
and matched them to the input String. They match exactly, character for character. So why is the equality not behaving correctly?
PS - The really really really weird thing about this problem I'm having is that I have the same type of approach earlier in the function, with the same exact input type and it works just fine. If I were to, for example remove "Here is a value" from the original CSV String, then the condition is met and myBool
becomes true (seems to be as long as the comparison value is the 1st element, then the condition holds; otherwise, it does not).
PPS - The individual values in the original CSV String will never have commas in them, so myString.split(", ");
works for what I am doing.
UPDATE: I separated the function into its own script file, and tested it. And here's where the bizarre behavior is (what exactly is String.split()
doing?
/*
* This is how the data is set up in my script. There is an Object that has a property which is a String.
* The object itself is passed as a paramter, and I check the
*/
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