3
votes

I'm at my wit's end when it comes to adding up the individual sums of arrays inside a two-dimensional array. For example:

var arrArrays = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

function sumArray(numberArray) {
  var sum = 0;
  numberArray.forEach(function(a,b) {
    sum = a + b;
  });
  return sum;
}

The function sumArray can successfully add up & return the sum of a regular (one-dimensional array), but when passed arrArrays it returns a single array of random-looking values.

I would need it to be able to return the sums of however many arrays are inside another array. The reason being is because I need this next function to call sumArray():

function sumSort(arrayOfArrays) {
  arrayOfArrays.sort(function(a,b) {
    var sumArray1 = sumArray(a);
    var sumArray2 = sumArray(b);
    if (sumArray1 < sumArray2) {
      return -1;
    } else {
      return 0;
    }
  });
}

sumSort() will, theoretically, order the arrays based on the sum of the numbers inside each array (descending from highest to lowest).

Any tips would be awesome. Thank you in advance!

1
I don't know what you think the arguments to forEach callback are, but they aren't what you think they are. In your function you are summing of the last element + the index of the last element because b is the index. - Matt Burland
@Mr.Llama: The OPs code doesn't work. - Matt Burland
@Mr.Llama - The code doesn't seem to work to me. The result is "7,8,92". - Travis J
"The function sumArray can successfully add up & return the sum of a regular (one-dimensional array), but when passed arrArrays it returns a single array of random-looking values." Based on the question it seems you don't actually want to pass an array of arrays to sumArray, but an array of numbers. Or are you passing an array of arrays of arrays of numbers to sumSort? - Felix Kling
Right. So you are actually passing arrays containing numbers to sumArray (because that's what arrArrays contains), not an array of arrays. Why do you think sumSort does not work? I mean there are issues as already mentioned. forEach doesn't work like that and your sort callback never returns 1. Your sort callback basically says "if sumArray1 is not smaller than sumArray2, then they are equal", which is certainly not the case for sumArray1 = 30 and sumArray2 = 10. Have a look at developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… . - Felix Kling

1 Answers

1
votes

This method will allow you to handle arrays of any nested depth. First you'll flatten the inner arrays and then sum them as usual. If you also want it to handle non-nested arrays, you can add a check on the first level, as shown in this question.

var arrs = [[[[1]], [2, 3]], [4, 5, 6], [7, 8, 9]];
//https://stackoverflow.com/a/15030117/1470607
function flatten(arr) {
    return arr.reduce(function(flat, arr) {
        return flat.concat(Array.isArray(arr) ? flatten(arr) : arr);
    }, []);
};
var sums = arrs.map(function(arr) {
    return flatten(arr).reduce(function(sum, item) {
        return sum + item;
    });
}); //[6, 15, 24]