Gurcan
Gurcan

Reputation: 438

AngularJS filter field according to values of an array

I have an Array and this array contains some Id's. I would like to filter my result according to Id's.

For Example please see the below code snippet. How can I show the records which 'StatusId' located in the 'ids' array? According to my example, I want to list only:

AAA BBB CCC

<html>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<script>
    angular.module('myApp', []).controller('ctrl', function ($scope) {

        $scope.ids = [{
            "id": 1
        }, {
            "id": 2
        }, {
            "id": 3
        }]

        $scope.records = [
            {
                "Name": "AAA",
                "StatusId": 1
            },
            {
                "Name": "BBB",
                "StatusId": 1
            },
            {
                "Name": "CCC",
                "StatusId": 2
            },
            {
                "Name": "DDD",
                "StatusId": 4
            },
            {
                "Name": "EEE",
                "StatusId": 4
            },
]


    });
</script>


<body>
    <div id="idctrl" ng-app="myApp" ng-controller="ctrl">
        <ul>
            <li ng-repeat="x in records">
                {{ x.Name }}
            </li>
        </ul>
    </div>
</body>

</html>

Upvotes: 1

Views: 32

Answers (1)

Mike G
Mike G

Reputation: 46

You can use the javascript 'filter' function:

records = $scope.records.filter((record) => {
    return record.StatusId === 1
});

This will return a new array that only contains records with a StatusId of 1. You can these use this updated array to display your desired records.

Upvotes: 1

Related Questions