AngularJS: find the index position of filtered value in the original array

Thilaga picture Thilaga · Dec 24, 2013 · Viewed 95.7k times · Source
$scope.data = [
    {
    "name": "Jim",
    "id" : 25
    },
    {
    "name": "Jerry",
    "id": 27
    },
    {
    "name": "Rithika",
    "id": 20
    }
    ];

    <div ng-repeat="person in data | filter: {id:20}">
        {{parent_index}}
    </div>

parent_index - Index of the filtered element in the actual array.

In this example, parent_index should return 2. how to find it?

Answer

Maxim Shoustin picture Maxim Shoustin · Dec 24, 2013

find the index position of filtered value in the original array

Try this one:

<div ng-repeat="person in data | filter: {id:20}">
    {{data.indexOf(person)}}
</div>

Output: 2

Demo Fiddle