-1

Task

You have a list of things (array) and you would like to modify all of them in the same way.

eg:

url_list = ['google.com', 'apple.com', 'amazon.com']

desired output is for example:

www_url = ['www.google.com', 'www.apple.com', 'www.amazon.com']

Issue/Question

What's the simplest way to update all of those values that work in Apps Script.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Landsil
  • 23
  • 10
  • 2
    What's the difference between input and output and what have you tried so far? – TheMaster Oct 14 '20 at 16:10
  • all well, I'm trying to post useful solution but don't have enough points yet to post it in one go :( – Landsil Oct 14 '20 at 16:23
  • [Edit] your question to update the "desired output". Currently both input and output are the same. And `forEach` does work in apps script. – TheMaster Oct 14 '20 at 16:48

3 Answers3

1

Simplest way seem to be by using "map" function.

 var www_url = url_list.map((url) => 'www.' + url);

All you do is map new value in the place of old value and you have quite a lot of option in how you can modify it.

It also takes 2nd argument so you can specify different change for some positions. In my case I needed 1st record to look different.

 var netWhitelist_values = url_list.map((url, index) => 
 {
 if (index === 0) return (url + '\\');
 return ('\\n' + url + '\\');
 });
Landsil
  • 23
  • 10
  • 2
    Okay. I just thought https://stackoverflow.com/questions/953311/replace-string-in-javascript-array or https://stackoverflow.com/questions/12482961/is-it-possible-to-change-values-of-the-array-when-doing-foreach-in-javascript or https://stackoverflow.com/questions/35879307/change-properties-of-every-item-in-an-array would all provide easily adaptable answers such that fresh self Q&A seems redundant to me. But... okay. – Marc Oct 14 '20 at 16:28
  • foreach doesn't work in appsscript. Should I just remove javascript from tags? normal loop would work yes, I was just looking at something simpler, also honestly haven't found those two. I must have been asking wrong question. – Landsil Oct 14 '20 at 16:35
  • @Landsil - excuse me, why would a normal `forEach` method not work in GAS? FYI, GAS *is* just JavaScript with a few twists (even more true since the shift to V8 runtime. Still, `forEach` worked in Rhino as well). Btw, if you want to build an initial rep, you can start by looking at completely unanswered questions and taking a shot at those you understand: https://stackoverflow.com/search?q=%5Bgoogle-apps-script%5D+answers%3A0 – Oleg Valter is with Ukraine Oct 16 '20 at 05:40
  • I was trying to use it and it didn't register, I've googled it and got same info. v8 migration manual also says it should be avoided in favour of just "for. ( found it just now) Yea, I will have to spend more time with new questions but looks like most of them get answers instantly. – Landsil Oct 23 '20 at 15:22
1

The solution posted by @Landsil should work just fine with Apps Script, but in case it doesn't, you can rewrite it in a more traditional way:

for(let i=0; i<url_list.length; ++i) {
      www_url[i] = 'www.' + url_list[i];
}
ptorr
  • 83
  • 6
0

You could do something like this:

let url_list = ['google.com', 'apple.com', 'amazon.com']

url_list.forEach(item => {
  console.log('www.' + item);
})
TheMaster
  • 45,448
  • 6
  • 62
  • 85