1

I'm pretty sure this is a basic question but my PHP background is making me unable to solve this. I was also unable to find a solution that came even close in SO.

I have an array of objects containing a value I want to loop and sum. Everything works well but I would like to directly sum those values to another array in the output.

So this is the working function:

    function postsCalc(arrayEntry) {
        var postsOutput = [];
        var postLikes = 0;

        for (var i = 0, len = arrayEntry.length; i < len; i++) {
          postLikes += arrayEntry[i].likes.summary.total_count;
        }   

        postsOutput.likes = postLikes;

        return postsOutput;     
    }

Output:

likes : 55555;

Which works well, but can't I push it directly to the array key and avoid having to do the postsOutput.likes = postLikes?

Like this:

    function postsCalc(arrayEntry) {
        var postsOutput = [];

        for (var i = 0, len = arrayEntry.length; i < len; i++) {
          postsOutput.likes += arrayEntry[i].likes.summary.total_count;
          // or even a multidimensional:
          postsOutput.totals.likes += arrayEntry[i].likes.summary.total_count;
        }   

        return postsOutput;     
    }

and that would output the same as above but avoiding the extra step, is this possible in Javascript?

Thanks in advance

1
  • 1
    In php you have associative arrays, but in javascript arrays are non-associative (only contain values, not keys). If you want keys - you are talking about objects (and not Arrays). Commented Jan 3, 2017 at 23:09

2 Answers 2

1

You can use the reduce function on a list to do this:

function postsCalc(arrayEntry) {
    var postLikes = arrayEntry.reduce(function(a, b) {return a + b.likes.summary.total_count}, 0);

    return {likes: postLikes};     
}

It works like this: Array.reduce(callback, initialValue) where callback = function(a, b) where a is the accumulator (e.g. tracks the sum, etc) and b is a representation of the item you're iterating over on the list. initialValue is the starting point for the function (e.g. on the first iteration).

Sign up to request clarification or add additional context in comments.

2 Comments

This worked like a charm, it's a very neat solution. I'm going to read more about .reduce because it seems very interesting, thanks!
Happy to help. The Array prototype methods are all very helpful once you get the hang of them.
1

Idiomatic javascript would look like (for your first function):

function postsCalc(arrayEntry) {
    var postsOutput = {
        postLikes: 0
    };

    for (var i = 0, len = arrayEntry.length; i < len; i++) {
      postsOutput.postLikes += arrayEntry[i].likes.summary.total_count;
    }   

    return postsOutput;     
}

or, now that we can assume .forEach exists:

function postsCalc(arrayEntry) {
    var postsOutput = {
        postLikes: 0
    };

    arrayEntry.forEach(function(entry) {
      postsOutput.postLikes += entry.likes.summary.total_count;
    }   

    return postsOutput;     
}

1 Comment

In your example I still have to create an empty object to assign the values later and that's what I was trying to avoid. The accepted solution is simpler but that you very much for your help!

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.