user1650978 user1650978 - 1 month ago 12
AngularJS Question

Check for multiple values in array object - AngularJS

I have the following :

$scope.Items = [{"ItemId": 1, "ItemName":"Test1"}, {"ItemId": 2, "ItemName":"Test2"}, {"ItemId": 3, "ItemName":"Test3"}];


If I wish to check if ItemId 1 AND ItemId 2 exist within this, what would be the best approach for it? I believe the
indexOf
property should work, but how can it be used to check if a particular attribute value of the object exists within the array?

Answer

you can use like also

$scope.firstitem=false;
$scope.seconditem=false;
angular.forEach($scope.Items, function (value, key) {
            if (value.ItemId== 1) { $scope.firstitem=true; }
            if (value.ItemId== 2) { $scope.seconditem=true; }
        });