0

Following this answer for an analogous question for vanilla JavaScript, I've tried to create an array of integers from 1 to 20 with the following code:

var k=Array.from(new Array(20), (x,i) => i + 1);

but I get Syntax error. Evidently Google App Script does not support ES6.

Is there any modern alternative to for ... loop?

tic
  • 4,009
  • 15
  • 45
  • 86

3 Answers3

4
  • You want to create an array including the number in each element using Google Apps Script.
    • It's like [1.0, 2.0, 3.0,,, 20]

If my understanding is correct, how about this workaround? Please think of this as just one of several workarounds. In this workaround, I used Array.apply().

Sample script 1:

var res = Array.apply(null, Array(20)).map(function(_, i) {return i + 1});
console.log(res); // or for Google Apps Script, Logger.log(res)

Sample script 2:

In the current stage (2022), the following sample script can be also used.

var res = [...Array(20)].map((_, i) => i + 1);
console.log(res);

Note:

  • For example, when Array.apply(null, Array(3)) and [...Array(3)] are run, an array of [undefined,undefined,undefined] is created. In this script, values are put in each element using map(). If you want to fill an array by a value, you can also use Array(3).fill("sample").

  • Above script can be worked with Google Apps Script.

  • If you want to use Logger.log(), please replace console.log(res) to Logger.log(res).

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • @Atiq Zabinski Thank you for your comment of `Your example isn't working for me. I get [undefined,undefined,undefined] regardless of whether I use null, a number or a string. Assuming that close-curly brace is a typo; GAS won't let me insert an open-curly brace anywhere amidst this. function testNewArray () { var result = Array.apply('whatever', Array(3)); console.log (result) }`. Unfortunately, I cannot replicate your comment. – Tanaike Dec 15 '22 at 00:16
  • @Atiq Zabinski But, I would like to support you. So, can I ask you about the detail of `Your example isn't working for me.` using your sample value? By this, I would like to confirm it. If you can cooperate to resolve your issue, I'm glad. Can you cooperate to do it? – Tanaike Dec 15 '22 at 00:16
  • @Atiq Zabinski Thank you for replying. I have to apologize for my poor English skill, again. Unfortunately, from `function testNewArray () { var result = Array.apply('whatever', Array(3)); console.log (result) }`, `What is unclear?`, `function testNewArray () { var result = Array.apply(null, Array(3)); console.log (result) } `, I cannot still understand your current issue. But, I would like to support you. So, can I ask you about the detail of your current issue? By this, I would like to confirm it. – Tanaike Dec 17 '22 at 01:40
  • @Atiq Zabinski Thank you for replying. Now, I understood your question. Your are right. `Array.apply(null, Array(20))` returns `[undefined,undefined,,,]`. In the current stage, `fill()` can be used. So, if you want to create `[null,null,,,]`, you can use `Array(20).fill(null)`. And also, in the current stage, I think that `var res = [...Array(20)].map((_, i) => i + 1);` can be also used. So, I updated my answer. Please confirm it. – Tanaike Dec 17 '22 at 01:57
  • Yes, perfect! Also, .fill was the method I had forgotten and actually was what I was looking for in the first place! – Atiq Zabinski Dec 17 '22 at 03:43
  • @Atiq Zabinski Thank you for replying and testing it. I'm glad your issue was resolved. Thank you, too. – Tanaike Dec 17 '22 at 23:20
2

Issue:

The main issue is that arrays created using new Array(length) is sparse and have elements that's never set and most array methods don't work on it.

Solution:

It's possible to create a dense array from sparse array using apply. Then, it's easy to get indexes of that array using Object.keys()

Snippets:

//using concat
function test11(){
  Array.prototype.range = function(len){
    return Object.keys(Array.prototype.concat.apply([],new Array(len)))//sparse to dense
  }
  Logger.log([].range(16))
}

//using push
function test12(){
Array.prototype.range = function(len){
  var out = [];
  Array.prototype.push.apply(out,new Array(len))
  return Object.keys(out);
}
Logger.log([].range(15))
}
Community
  • 1
  • 1
TheMaster
  • 45,448
  • 6
  • 62
  • 85
2

No. for/while loop is still best (performant) even on modern browsers. A small function can be used :

function range(n) { var a = []; while(n) a[n - 1] = n--; return a }

console.log( range(5) )
Slai
  • 22,144
  • 5
  • 45
  • 53