简体   繁体   中英

I get a syntax error when I try to use array map function in Google Scripts. Why?

I get a syntax error when I insert this particular line

pp = pp.map(x => precise(x));

into my code. No other line gets the same error, and the code runs fine without this line. Please note that I am using google scripts editor.

pp is an array.

The function precise is coded like this.

function precise(x) {
  return Number.parseFloat(x).toPrecision(3);
}

I have isolated the code and tried it out in another online JavaScript editor, which works without a problem.

Is it a problem with Google Apps Script?

Are you sure that Google Apps Scripts is compatible with ES6 syntax such as arrow functions?

You could try changing your code to the following and see if it solves the issue:

pp = pp.map(function(x) { return precise(x) });

As the method precise returns the string, You can directly pass the function reference to .map() method

pp = pp.map(precise);

or, You could do

pp = pp.map(function(x) { return precise(x) });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM