How can I sum numbers in an array?

Any quick tip on how to sum numbers in an array?

image

I would like to add a column that sums the numbers in the array of numbers in the last column. The array is a simple list of numbers, appended by the row.

Use a rollup column

1 Like

Nope. Doesn’t work… the sum option is not available.

image

Related: I would like the count of instances of an item from an array.

array

Interesting! :thinking:

I didn’t know that a Rollup column can count the items of array in each row, thanks for the tip!

However, your goal is the sum right?.. I think a customized JS code can do it easy:

This is my code by using an EC:

let len=p8.length;
let x=0, total =0;

while (x< len)  {
   total+= parseInt(p8[x]);
   x++ ;
}
return total

The problem here is that Glide’s JavaScript plugin does not support an array as input value so you must use another version or create a new version of that JavaScript plugin.

Maybe @david can read this note and carry out an improvement of his plugin to support these cases.

My version to work with this case runs on Replit:

https://SNB-YesCode.gustavovalero.repl.co

Hope it helps you!

Feliz noche @Simon_Hill

4 Likes

What are you using to get that rolling total array? Seems like it’s being read as text.

1 Like

Right!

It is the reason why I use parseInt() in the code. This covers all cases so far.

Edited
I already know what you mean Thind, of course, if the arrays have numeric values the Rollup operation would have worked perfectly!!

Saludos!

2 Likes

This seems like an oversight. @mark @david?

1 Like

What is this Rolling total array column? How is it configured? Glide does not recognize it as an array of numbers.

Hola @BlakeWS

You can try with this JS code until Glide’s JavaScript plugin can support arrays as input values (parameter)

let  len=p8.length;
let  total=0;

for (let x= 0; x< len; x++) {
   if (p1==p8[x])  
      total ++
}
return total

My Source’s URL on Replit is :

https://snb-yescode.gustavovalero.repl.co

Saludos!

2 Likes

@gvalero or:

return p8.filter(x => x === p1).length

:wink:

6 Likes

Right!

I had forgotten the Filter() method :woozy_face:

Sometimes I like the old-programming school (C++) :grinning:

Gracias por el tip @david

1 Like

Thanks @gvalero & @david , it works great.

2 Likes

Hi David, thanks for taking the time.

Here is a quick video to explain… I think you’re right, Glide does not recognize the array is comprised of numbers.

Any quick fix?

1 Like

hmm, I’m surprised that isn’t working. Will be interested to see what the outcome is…

Interesting to note that the roll-up with sum works on the full array of 12 numbers. It stops working after splicing the array.

1 Like

yeah, kinda suggests that the Array Splice isn’t actually returning an array.

4 Likes

Hola Darren,

I think the Slice Array plugin is doing its job but the result is always a Text therefore, the Rollup column can’t sum text values.

Bye

yes, that’s what I meant. It’s not returning an array.

Yes! I bet the array columns all deal with arrays of ‘primitives’, which allows them to work on arrays of anything, but this causes the resulting array to lose type information (the Slice Array column ‘forgets’ what kind of array it’s slicing).

In regular programming, the solution to this is called Generics or polymorphism. I have some research projects to discuss with @Ivo_Elbert about this soon.

4 Likes