Magini Magini - 5 months ago 182
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>
</div>


and have the size
{{items.length}}


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

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


but how to establish the size?

Any help is appreciated.

Answer

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) }}
Comments