I've got a list of folder names corresponding to file names within each of these folders.
I'd like to delete the duplicate folder names. This is what I need it to end up as:
My thought was that I would do a reverse loop comparing each entry to the one before it. If it matches add a blank '' to the top of an array. If it doesn't match then add the value of the array entry to the top of the array.
Below is my attempt. For some reason all of the comparisons are false. What am I doing wrong?
//Delete Duplicate folder names
var destRange = summarySht.getRange(startRow + 1,2,detLastRN - startRow,1);
//var folderNamesArr = destRange.getValues();
var folderNamesArr = [['Folder1'],['Folder1'],['Folder2'],['Folder2'],['Folder3'],['Folder4'],['Folder5'],['Folder6'],['Folder6']];
var updatedFolderNamesArr = [];
for (i = folderNamesArr.length - 1; i >= 0; i--) {
if(folderNamesArr[i] != folderNamesArr[i - 1]){
updatedFolderNamesArr.unshift(folderNamesArr[i])
}else{
updatedFolderNamesArr.unshift([''])
}
Logger.log(folderNamesArr[i] + ' vs ' + folderNamesArr[i - 1] + ' = ' + (folderNamesArr[i] === folderNamesArr[i - 1]))
}
Logger.log(updatedFolderNamesArr)
destRange.clearContent();
destRange.setValues(updatedFolderNamesArr);
I don't have many rows. I'm thinking worst case scenario we'll have 100 maybe 200 rows to process.
Update:
I added .toString() and it seems to work. Here is what I did and my issues with it.
//Delete Duplicate folder names
var destRange = summarySht.getRange(startRow + 1,2,detLastRN - startRow,1);
//var folderNamesArr = destRange.getValues();
var folderNamesArr = [['Folder1'],['Folder1'],['Folder2'],['Folder2'],['Folder3'],['Folder4'],['Folder5'],['Folder6'],['Folder6']];
var updatedFolderNamesArr = [];
for (i = folderNamesArr.length - 1; i >= 1; i--) {
if(folderNamesArr[i].toString() !== folderNamesArr[i - 1].toString()){
updatedFolderNamesArr.unshift(folderNamesArr[i])
}else{
updatedFolderNamesArr.unshift([''])
}
Logger.log(folderNamesArr[i] + ' vs ' + folderNamesArr[i - 1] + ' = ' + (folderNamesArr[i] === folderNamesArr[i - 1]))
}
//Add back the first folder
updatedFolderNamesArr.unshift(folderNamesArr[0]);
Logger.log(updatedFolderNamesArr)
destRange.clearContent();
destRange.setValues(updatedFolderNamesArr);
The first issue I had was that folderNamesArr[i - 1] doesn't work when it gets to the last element. So I had to change the limit on the loop to i >= 1. That then caused the issue of there being 1 less element. The first element was missing so I had to add it back to the end.
This seems so hacky to me. Is this acceptable or are there better ways/methods?

