3

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!

Jose
  • 4,880
  • 8
  • 27
  • 49
  • 2
    I don't know what you *think* the arguments to [forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/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 Sep 29 '15 at 21:11
  • @Mr.Llama: The OPs code doesn't work. – Matt Burland Sep 29 '15 at 21:13
  • 1
    @Mr.Llama - The code doesn't seem to work to me. The result is `"7,8,92"`. – Travis J Sep 29 '15 at 21:13
  • *"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 Sep 29 '15 at 21:14
  • Put differently, I believe the issue is that you are using `forEach` incorrectly, not that `sumArray` is not able to process nested arrays. – Felix Kling Sep 29 '15 at 21:21
  • @FelixKling I might have worded my questions strangely. Basically I would be passing the array "arrArrays" to sumSort(), and from there it would take the array of arrays (which by calling sumArray on it) would sort those by the sum in each. – Jose Sep 29 '15 at 21:23
  • 1
    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 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort . – Felix Kling Sep 29 '15 at 21:24

1 Answers1

1

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]
Community
  • 1
  • 1
Etheryte
  • 24,589
  • 11
  • 71
  • 116