gries
gries

Reputation: 260

Pass required argument plus variable length arguments to function

I have a function that has one required parameter and a variable number of parameters. I'm passing an array of blobs to the functions. A simplified version of the function looks like this:

function test(directory, blob0, blob1) {
  for (var argumentIndex = 1; argumentIndex < arguments.length; argumentIndex++) {
    var bytes = arguments[argumentIndex].getBytes();
    //do some things
  }
}

I'm using the rhino runtime so I can't use the spread operator. The closest I've come to making it work is by using the apply function, but I'm not sure how to also pass the required parameter (directory) in the test function above.

var blobs = [];
blobs[0] = getBlob();
blobs[1] = getBlob();
blobs[2] = getBlob();  

test.apply(null,blobs)  //doesn't set required parameter (directory)

Upvotes: 1

Views: 123

Answers (2)

Tanaike
Tanaike

Reputation: 201513

  • You want to use an array of blobs as the expanded values of blob0, blob1, blob2 at test(directory, blob0, blob1, blob2).
  • You want to use directory at test(directory, blob0, blob1, blob2) by adding directory.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

In this answer, bind is used.

Modified script:

// sample getBlob(). This returns a blob.
function getBlob() {return Utilities.newBlob("sample", MimeType.PLAIN_TEXT)}

function test(directory, blob0, blob1, blob2) {
  Logger.log("%s, %s, %s, %s", directory, blob0, blob1, blob2) // <--- directory, Blob, Blob, Blob

  for (var argumentIndex = 1; argumentIndex < arguments.length; argumentIndex++) {
    var bytes = arguments[argumentIndex].getBytes();
    //do some things
  }
}

// Please run this function.
function myFunction() {
  var directory = "directory";

  var blobs = [];
  blobs[0] = getBlob();
  blobs[1] = getBlob();
  blobs[2] = getBlob();

  test.bind(this, directory).apply(null, blobs);
}
  • In this modified script, directory is added using bind.
  • When myFunction() is run, you can see directory, Blob, Blob, Blob at Logger.log("%s, %s, %s, %s", directory, blob0, blob1, blob2) in the function test(directory, blob0, blob1, blob2).

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Upvotes: 4

izambl
izambl

Reputation: 659

Use the unshift() function, it adds an element to the begining of an array

var blobs = [];
blobs[0] = getBlob();
blobs[1] = getBlob();
blobs[2] = getBlob();

blobs.unshift(directory)

test.apply(null,blobs)

Upvotes: 1

Related Questions