'How can I make these strings into decimals using map (GAS)?

I'm trying to make sure that the resulting column will onlu contain numbers, but it keeps coming as string.

function moveColsWesbanco() {
  var ss = SpreadsheetApp.getActive();
  var sourceSheet = ss.getSheetByName('Sheet1');
  var destSheet = ss.getSheetByName('New Entry');
  var debitsCredits = sourceSheet.getRange('A7:D');
  var values = debitsCredits.getValues();
  let amounts2 = values.filter(o => o[0] != '').map(e => e[2] != '' ? e[3] = Number.parseFloat(e[2]) : Number.parseFloat(e[3]));
  let allTypes = [];
  for (val in amounts2) {
    allTypes.push(typeof val)
  };
  Logger.log('Types within it: ' + allTypes)
  return;
}

Here are the logs: enter image description here

So...I wonder what I'm missing here. Thank you!



Solution 1:[1]

I think that when "for in" is used like for (val in amounts2) {}, val is the index. And, it's the string type. I think that this is the reason of your issue. In this case, how about using "for of" as follows?

Modified script:

for (var val of amounts2) {
  allTypes.push(typeof val)
};

References:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tanaike