IMPORTANT: reduceRight with and without v8 runtime for Google Apps Script

Gists

This is an important point for using reduceRignt with and without v8 runtime for Google Apps Script.

Sample script

function myFunction() {
  var array = ["a", "b", "c", "d", "e"];
  var res = array.reduceRight(function (ar, e, i) {
    ar.push([e, i]);
    return ar;
  }, []);
  Logger.log(res);
}

Result

With V8

When V8 runtime is used, the following result is obtained.

[["e",4],["d",3],["c",2],["b",1],["a",0]]

Without V8

When V8 runtime is NOT used, the following result is obtained.

[["e",0],["d",1],["c",2],["b",3],["a",4]]

Summary

When above results are compared, it is found that the indexes are different. The indexes of “With V8” is in the opposite direction to that of “Without V8”. Please be careful this.

When I used the script including reduceRight for Rhino runtime (without V8) with enabling V8 runtime, an error occurs and I had spent for the time to find the reason due to reduceRight. So I would like to introduce this as an important point.

Reference

 Share!