Magini Magini - 8 months ago 280
AngularJS Question

How to count map size in AngularJS

I have a map with key values as follows

$scope.items = {
0={"name": "Jim", "age" : 25}
1={"name": "Eric", "age" : 25}

if it was an array to count the size I with do:

<div ng-repeat="item in items>

and have the size

In case of a map I with iterate items as follows:

<div ng-repeat="(id, item) in items">

but how to establish the size?

Any help is appreciated.


One approach is to calculate number of keys in the object using Object.keys:

$scope.itemsLength = Object.keys($scope.items).length;

you can also define a helper function:

$scope.getLength = function(obj) {
    return Object.keys(obj).length;

and use it in template:

{{ getLength(items) }}