I'm working with angular, and i need to be able to display an array length but without using ng-repeat.
here's the scenario:
I have default.json:
{
{ ...
"data":
[{
"name":"Test",
"errors":
[
{"id": 1,"level": 2,"details": {...}},
{"id": 3,"level": 1},
{"id": 5,"level": 1},
{"id": 5,"level": 1},
....
],
},
{
"name":"Test",
"errors":
[
{"id": 1,"level": 2,"details": {...}},
{"id": 5,"level": 1},
....
],
}
],
....
}
}
then I've created myData from the controller, so that I can do something like ng-repeat="data in myData" and i can use it properly.
Now, here's the question:
I Need to have a recap of all the errors, a sum of all the errors in the objects.
I tried to do something like this:
<span ng-repeat="data in myData">
<i ng-repeat="error in data.errors">{{error.length}}</i>
</span>
and nothing was printed. So I tried:
<span ng-repeat="data in myData">
<i>{{data.errors.length}}</i>
</span>
and the result was:
4 2 0 0 0 0 0
and it makes sense, because my first object as 4 errors, my second one 2 and so on...
But what I'd like to see is only: 6, as there are 6 errors in total.
How can I get only one value that sums all the errors from all the objects I've got without having to use ng-repeat (I think I don't need it there)
Hope this question is clear enough, thanks for your help.
forEachfunction or whatever you desire.{{data.errors.reduce( (prev, curr) => prev + curr )}}