user2024080
user2024080

Reputation: 5101

How to filter `objects` by it's value

In my angular controller, I would like to filter and assign some object with true value. i am trying to iterate using angular.forEach but i am getting all object as result instead of getting the truthy object.

here is my code :

$scope.splash.$promise.then(function (result) {

            $scope.allApps = result; //50 apps.
            // splashAppsHandler();

            $scope.splashApps = angular.forEach( $scope.allApps, function (app) {

                return app.projects.project.splash === true; //only 5 apps

            });

            console.log($scope.splashApps); //getting all 50 apps!?


        });

What is the correct way to do it?

Upvotes: 0

Views: 40

Answers (1)

Muhammad Reda
Muhammad Reda

Reputation: 27023

You need to store truthy objects with in an array or object. Currently you are not doing inside angular forEach.

$scope.truthyObjects = [];
$scope.splash.$promise.then(function (result) {

        $scope.allApps = result; //50 apps.
        // splashAppsHandler();

        $scope.splashApps = angular.forEach( $scope.allApps, function (app) {

            if(app.projects.project.splash === true) {
                 $scope.truthyObjects.push(app); // save truthy object
            }

        });

        console.log($scope.splashApps); //getting all 50 apps!?


    });

Upvotes: 1

Related Questions