I am trying to compare string values that I have obtained from my google spreadsheet.
My Months appear in this format " Jun13", "Aug13" etc. and my script has trouble comparing the values when they are in such formats
var data = ss.getRange(2,2,ss.getLastRow(),3).getValues();
var ListOfMonths = new Array();
for(var i = 0; i < data.length; ++i){
var row = data[i][0];
var duplicate = false;
for(j in ListOfMonths){
if(row.toString.match(ListOfMonths[j][0])){
duplicate = true;
}
if(!duplicate){
ListOfMonths.push(row);
}
}
}
Could someone help me with this issue of comparing strings? I also tried == and === but they all don't work
Just use '==' operator:
Enjoy!
Is there any reason you can't use the '==' operator?
try that:
Harold's code is more efficient than yours but it has a different logic... if you're interrested to know why your code didn't work here is a version that takes the same approach as yours but with a few bug corrections that make it work.
Shortly described, you pushed the value at a wrong place in the loop and you forgot to add a first item to have at least one element in your second array. You didn't use match the right way, match returns the 'common' part in the string, not a boolean.
Here is the code :