1

I've an array which I got from a spreadsheet by .getValues() method:

var arr = [
[12, 123euro, 13],
[345, 432euro, 293],
]

I want to remove 'eur' by mapping like this:

var newarr = arr.map(row => row[1].replace('euro',''))

But this gives me:

[123, 432]

Rewriting the line as:

newarr = arr.map(row => [row[0], row[1].replace('euro',''), row[2])

gives me the desired newarr:

var arr = [
[12, 123euro, 13],
[345, 432euro, 293],
]

I was expecting this result from the first try. What am I missing?

3
  • What is 123euro and 432euro? Are they strings? Commented Mar 9, 2021 at 21:06
  • @evolutionxbox, yes Commented Mar 9, 2021 at 22:26
  • In your first try you are returning a single element per row and that is row[1].replace('euro',''). Commented Mar 9, 2021 at 23:05

2 Answers 2

5

Array.prototype.map() expects a single return value from each iteration which is assigned to the index of that iteration in the array on which it is called.

Your first attempt arr.map(row => row[1].replace('euro','')) returns the return value of your .replace() call, which is a string, and replaces the entire iterated row with it.

Instead, you want to assign that returned value back to row[1] and then return the entire row. (Here using a comma operator and converting the returned string to an integer using a unary plus(+)).

arr.map(row => (row[1] = +row[1].replace('euro', ''), row))

It should be noted that mutating the row array this way will also mutate the original. To avoid this you'll need to make a copy, either with slice() or by using spread syntax (or by building an entirely new array as you did in your working example).

var arr = [
  [12, '123euro', 13],
  [345, '432euro', 293],
];

var newarr = arr.map(row => {
  const newRow = [...row];
  newRow[1] = +newRow[1].replace('euro', '');
  return newRow;
})

console.log(newarr);

Sign up to request clarification or add additional context in comments.

3 Comments

That works, thank you! Can you please explain the outer parenthesis? (row[1] = +row[1].replace(curToken,''), row) and this part: , row
It wraps the two expressions which are separated by the comma operator in order to implicitly return the result after all are evaluated. It is equivalent to arr.map(row => { row[1] = +row[1].replace('euro', ''); return row; }); using a multiline callback with an explicit return value.
I want to know more about this interesting syntax. What should I search for in, say, MDN?
0
function myfunc() {
  var arr = [
    [12, '123euro', 13],
    [345, '432euro', 293]
  ];
  let arr1 = arr.map((r) => {
    return [r[0], r[1].replace(/euro/g, ''), r[2]];
  });
  Logger.log(arr1);
}

Execution log
4:07:32 PM  Notice  Execution started
4:07:32 PM  Info    [[12.0, 123, 13.0], [345.0, 432, 293.0]]
4:07:32 PM  Notice  Execution completed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.