-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.

3
  • 2
    What's the difference between input and output and what have you tried so far? Commented Oct 14, 2020 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 :( Commented Oct 14, 2020 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. Commented Oct 14, 2020 at 16:48

3 Answers 3

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 + '\\');
 });
Sign up to request clarification or add additional context in comments.

4 Comments

Okay. I just thought stackoverflow.com/questions/953311/… or stackoverflow.com/questions/12482961/… or stackoverflow.com/questions/35879307/… would all provide easily adaptable answers such that fresh self Q&A seems redundant to me. But... okay.
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 - 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: stackoverflow.com/search?q=%5Bgoogle-apps-script%5D+answers%3A0
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.
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];
}

Comments

0

You could do something like this:

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

url_list.forEach(item => {
  console.log('www.' + item);
})

2 Comments

this was actually my original plan but apps script version of java script doesn't have this function :(
Foreach does work in apps script. Don't know what OP did wrong with forEach.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.