Skip to content
Advertisement

Sum column values from multiple arrays

I have an arrays with dynamic name. My array could be more than 3, depends and array variable should be unique

JavaScript

I need to get an output like this:

JavaScript

Advertisement

Answer

Why aren’t you using a multidimensional array?

JavaScript

In this case you will have an array of arrays:

JavaScript

You can go with nested for loops:

JavaScript

The output is

JavaScript

Now, if you have a disproportioned sub-arrays (i.e. different count of elements in each sub-array), you will still get some sort of result, as missing elements will be assumed to be 0. So, with the input of:

JavaScript

You will still get the result:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement