homerboy homerboy - 4 months ago 14
CoffeeScript Question

Aggregate likes from an array of objects

I'm trying to aggregate the number of likes from all Facebook pages of a user. I'm using a

map
to go through each of the user's pages, grab the likes of that object, and add it to a variable.

{_(@state.fbPages).map((x) -> return likes: x.likes)}


But right now it's only adding all the likes in a row like this: 32049381. Is there something in the underscore.js library (or any other way) that can help me individualize these likes and aggregate them?

Answer

Are you saying that it is adding all the likes into a string? i.e. is it taking an array like this [3,2,0,4,9,3,8,1] and your result is 32049381? It may be storing individual likes as strings, and you will need to use parseInt().

{_(@state.fbPages).map((x) -> return likes: parseInt(x.likes))}

I am sort of confused by your question but hopefully this is what you were looking for