0

I have four values with names :

$scope.restaurant.restaurantIsTop
$scope.restaurant.restaurantIsRecommended
$scope.restaurant.restaurantIsNew 
$scope.restaurant.restaurantIsPromoted

Each of them can be 0 or 1 . I want to check them if their value is equal to 1 , change it to true , otherwise it would be false.

I could do this with this method for four variable:

        if ($scope.restaurant.restaurantIsTop == 1  ?
                $scope.restaurant.restaurantIsTop = true :
                $scope.restaurant.restaurantIsTop = false);
        if ($scope.restaurant.restaurantIsNew == 1  ?
                $scope.restaurant.restaurantIsNew = true :
                $scope.restaurant.restaurantIsNew =false);
        if ($scope.restaurant.restaurantIsRecommended == 1  ?
                $scope.restaurant.restaurantIsRecommended = true :
                $scope.restaurant.restaurantIsRecommended =false);
        if ($scope.restaurant.restaurantIsPromoted == 1  ?
                $scope.restaurant.restaurantIsPromoted = true :
                $scope.restaurant.restaurantIsPromoted =false);  

Absolutely it works but It's not efficient way for multiple value. So I created an array :

var varietyArray = ["restaurantIsTop" ,
                                "restaurantIsRecommended",
                                "restaurantIsNew",
                                "restaurantIsPromoted"

            ];

        angular.forEach (varietyArray, function (val) {

            console.log($scope.restaurant.val);
        })

And I want to check four variable in above loop . Any suggestion ?

EDITED :

I want to show them in Angular Material checkbox :

                    <div class="col-sm-12">
                            <div class="md-form-group">
                                <md-checkbox ng-model="restaurant.restaurantIsTop ">
                                    best
                                </md-checkbox>
                                <md-checkbox ng-model="restaurant.restaurantIsRecommended">
                                    suggested
                                </md-checkbox>
                                <md-checkbox ng-model="restaurant.restaurantIsNew">
                                    new
                                </md-checkbox>
                                <md-checkbox ng-model="restaurant.restaurantIsPromoted">
                                    promote
                                </md-checkbox>
                            </div>
                    </div>
4
  • 2
    0 and 1 is by default falsy and truthy . if you make changes on same variable, it'll changes it's meaning. You wanna display on view true or false ? or it's just for internal calculation ? Commented Sep 18, 2016 at 6:37
  • If its just 4 of such values, If Else is perfect. Avoid using ternary operator if possible.. Here is the link. stackoverflow.com/a/12022491/1907391 Commented Sep 18, 2016 at 6:44
  • Moreover 0 is false and 1 is true ultimately. Commented Sep 18, 2016 at 6:44
  • So if you are sending this $scope.resturant object to server, it automatically won't receive the unchecked ones. Commented Sep 18, 2016 at 6:45

2 Answers 2

1

To check the variable within your loop and set it conditionally you can to it this way:

var $scope = {
  restaurantIsTop: 1,
  restaurantIsRecommended: 0,
  restaurantIsNew: 0,
  restaurantIsPromoted: 1
};

var varietyArray = ["restaurantIsTop" ,
                            "restaurantIsRecommended",
                            "restaurantIsNew",
                            "restaurantIsPromoted"

        ];

angular.forEach (varietyArray, function (val) {

  $scope[val] = ($scope[val]) ? false : true;
  console.debug($scope[val]);
})

You can access the scope variable with $scope[VariablenameAsString]

jsfiddle

Sign up to request clarification or add additional context in comments.

Comments

0

As someone already commented you don't need to change the value of those properties. 0 is equivalent to false and 1 is equivalent to true.

The Material checkbox should interpret those values as "falsy" or "truthy" so I think you should be fine at least in presenting the data through the checkboxes.

Nonetheless keep in mind that the checkboxes would "write" proper bool values in the model (property) when they change and you could end up with mixed 0, 1, true, false values.

Hope this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.