0

I have a collection of array-like objects like this :

a = [[1,2],[3,4],[5,6],[7,8]]

And I would like to sum the columns (if u think of this as a 4x2 array) such that:

col1_sum = 16
col2_sum = 20

What is the best way of doing this is JS?

I tried using underscore's _.reduce function like so:

var col1_sum =_.reduce(a, function(memo,obj){ return memo + parseFloat(obj[0]);},0)

but I get an "undefined" error

any ideas? thank you.

5 Answers 5

3

You could always kick it old school. It's not pretty, but it works.

col1_sum = 0;
col2_sum = 0;
for (var i = 0; i < a.length; ++i) {
    col1_sum += a[i][0];
    col2_sum += a[i][1];
}

My other thought was to use jQuery's each function, but I guess you're not looking for a jQuery solution?

EDIT - Who needs jQuery.each? Just use JavaScript's Array.forEach:

var col1_sum = 0;
var col2_sum = 0;

a = [[1,2],[3,4],[5,6],[7,8]];
a.forEach(function(element, index, array) { col1_sum += element[0]; col2_sum += element[1]; });

alert(col1_sum);
alert(col2_sum);
Sign up to request clarification or add additional context in comments.

3 Comments

Who needs jQuery.each? Probably people that have jQuery and don't want to shim Array.forEach support for IE<=8.
@FabrícioMatté I know, I know. I was being flippant. I use jQuery.each so much that I had forgotten about Array.forEach.
Heheh true, though I use jQuery's each/map/grep a lot, the ES5 array methods are very nice and useful when developing new applications.
3

I made a simple jsfiddle from your underscore reduce code, and it seems to be working fine: http://jsfiddle.net/PdL5P/

var a = [[1,2],[3,4],[5,6],[7,8]]

var col1_sum = _.reduce(a, function(memo,obj){ return memo + parseFloat(obj[0]); }, 0 );

$("#sum").html(col1_sum)

Are you sure that "a" is defined at that point of your code?

Comments

1

You can also kick it new school:

var sumColumnJS = function sumColumnJS(array, col) {
    var sum = 0;
    array.forEach(function (value, index, array) {
        sum += value[col];
    });
    return sum;
};

or use the _.each array iterator:

sumColumn_ = function sumColumn_(array, col) {
    var sum = 0;
    _.each(a, function (item) {
        sum += item[col];
    });
    return sum;
};

Working demo: http://jsfiddle.net/HTqvf/

Comments

0

Simple iteration, as in any traditional language

    var col1_sum = 0, col2_sum = 0, row;
    for (row in a)
    {
        col1_sum += a[row][0];
        cos2_sum += a[row][1];
    }

1 Comment

You probably shouldn't use for...in on an array, though. See, e.g. stackoverflow.com/questions/500504/…
-1

I would do this.

a = [[1,2],[3,4],[5,6],[7,8]]
col1_sum = _.sum(_.map(a,0))
col2_sum = _.sum(_.map(a,1))

1 Comment

user wants solution in underscore js. _.sum() is not underscore.js function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.