Home > Software design >  In GAS, getValues() is returning an array of arrays (I think...!)
In GAS, getValues() is returning an array of arrays (I think...!)

Time:04-18

Sorry... I have limited programming knowledge, so I'm rather hacking a script together and have got stuck.

For the following code (where the pupils is definitely being calculated correctly)

var dataRange = sheet1.getRange(2, 3, pupils, 1);
var ArrayOfValues = dataRange.getValues();

In the debugger, Logger.log(ArrayOfValues) gives:

[[80.0], [51.0], [57.0], [76.0], [A], [53.0]]

I then want to calculate the max/min (which is all working fine if ArrayOfValues is just numbers).

However, in the code above, I really want to first try to filter out non-numbers with

var filteredValues = ArrayOfValues.filter(function(value) {
   return typeof value === "number";
});

However, Logger.log(filteredValues) shows that filteredValues is an empty array. Basically, I'm writing something for colleagues to perform some stats on student scores and want to make sure I can account for anyone who has an A down for their mark as they were absent.

Many thanks,

Dave.

CodePudding user response:

var filteredValues = ArrayOfValues.filter(function(value) {
   return typeof value === "number";
});

When you in this filter loop, the value is [80.0], [51.0], [57.0], [76.0], [A], [53.0]. typeof value will be object.

Change to:

var filteredValues = ArrayOfValues.filter(function(value) {
   return typeof value[0] === "number";
});
console.log(filteredValues)

will return the correct type of each element on the arr. It should work.

  • Related