Babaev
Babaev

Reputation: 55

How to save index in array after filtering in Angular JS?

I have array of objects in Angular JS.

For showing this array I use ng-repeat with filter by value from input field.

Problem is that when I typing text in field ng-repeat makes filtering and changes indexes of array.

Default array before filtering:

0 => obj(o)
1 => obj(b)
2 => obj(c)

If I type "b" I get output list:

0 => obj(b)

But with index 0, if element is one. So index 0 had obj(o) before filtering.

How I can save indexes that will be 1 => obj(b) after filtering?

Upvotes: 1

Views: 658

Answers (1)

debugall
debugall

Reputation: 310

 $scope.defaultArray = [
        0 => obj(o)
        1 => obj(b)
        2 => obj(c)
     ];
     $scope.filteredArray = $scope.defaultArray;

Use filteredArray variable in your ng-repeat Then for every element remaining in filteredArray you can get its index by doing this : $scope.defaultArray.indexOf(obj(b)) if I search "b"

Upvotes: 1

Related Questions