Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sum on array #191

Closed
moshfeu opened this issue Feb 23, 2016 · 5 comments
Closed

add sum on array #191

moshfeu opened this issue Feb 23, 2016 · 5 comments

Comments

@moshfeu
Copy link

moshfeu commented Feb 23, 2016

This should looks like:

controller
$scope.users = [ { user: { score: 988790 } }, { user: { score: 123414 } }, { user: { score: 987621 } } ];

view
<p> {{ users | sum: 'user.score' }}</p>

result

<p>2099825</p>

Similar to min filter

Thanks

@toddbranch
Copy link
Collaborator

I think this might work:
{{users | map: 'user.score' | sum}}

@moshfeu
Copy link
Author

moshfeu commented Feb 24, 2016

Thanks! Works like charm ;)

@moshfeu moshfeu closed this as completed Feb 24, 2016
@moshfeu
Copy link
Author

moshfeu commented Apr 10, 2016

@toddbranch I have another question with your permission. My data is array of arrays like this:

$scope.data = [[1460232000000,2],[1460235600000,5],[1460239200000,7]];

My final goal is to sum the second number of each array, for this data 14.

I know that I need to do sum on map but I can figure how.

I started with the map on ng-repeat but it's show the full array.

<div data-ng-repeat="aa in data track by $index | map: 'aa[0]' ">{{aa}}</div>

Here is my poor result in a bin:

http://jsbin.com/laheho/edit?html,js

Thanks!

@moshfeu moshfeu reopened this Apr 10, 2016
@toddbranch
Copy link
Collaborator

Hi @moshfeu. Here's a solution:

// in controller
$scope.secondElement = function(item) {
  return item[1];
};

// in HTML
<div data-ng-repeat="aa in data | map: secondElement track by $index ">{{aa}}</div>

If there's a way to make an angular expression that accesses the second element of the array, this would be a bit simpler. But I can't figure out if that's possible.

@moshfeu
Copy link
Author

moshfeu commented Apr 11, 2016

Thanks! I got this from the docs but I hoped that this is can be done by expression. Pretty annoying. Thank you any way. I really appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants