I'm making a script to loop in a Google Drive folder and noticed I don't know what's the best way to loop over iterators (specifically FolderIterator and FileIterator). My first approach
const drive_id = '__________';
let base_folder = DriveApp.getFolderById(drive_id),
sub_folders = base_folder.getFolders();
Logger.log(base_folder.getFolders());
for(let sub_folder of sub_folders)
{
Logger.log(sub_folder);
}
outputs Error TypeError: sub_folders is not iterable.
Secondly I tried with while loops and checks on hasNext(), but I didn't find them beautifyl enough. I'm currently set with this for loop, that works and is clean enough but still looks a bit hacky.
/*...*/
for(let sub_folders = base_folder.getFolders(); sub_folders.hasNext();)
{
let sub_folder = sub_folders.next();
Logger.log(sub_folder);
}
Also notice how I couldn't declare sub_folder within the loop itself. So, what's the proper way of doing it?
Also, what if I wanted to use a map or filter on the sub_folders instead?
Note: this question is probably of broader scope than google-script, but I couldn't find the right terminology to ask the question differently so I preferred to stick to the particular case I found.