0

I am trying to compare a current object to an array of id's coming in. The basic idea is that if the object has the same idea as anything inside the recived ID array, then I would like to set a boolean of selected to true. I was pointed in the direction of using a for each with an indexOf inside to check against. Here is my Attempt -

angular.forEach($scope.applicationsHere, function(index) {
                    if(data.applications.indexOf(index.id){
                        index.selected = true;
                    }
                });

So what I am tyring to do is check the applications here against the data.applications. If the applicationsHere has an object with .id that matches one of the numbers in data.applications (data.applications is just an array of ids like [1,2,3]), then set the .selected to equal true.

I do not believe I have this logic correct, if anyone could help correct me I would much appreciate it. Thanks for reading!

5
  • Just to be clear, you're using angular, correct? I've added the tag in because vanilla JS does not have access to a $scope object. Commented Nov 3, 2014 at 16:07
  • The .indexOf() function doesn't return a true/false value - it returns the index. If the item is not found, it returns -1. Commented Nov 3, 2014 at 16:07
  • @Pointy he may be trying to use the JS coercion where anything non-zero returns true. The problem with that is that you can get an indexOf 0 that returns false. And I don't remember what happens with -1. Commented Nov 3, 2014 at 16:08
  • @Compass right, the possibility of a zero return value is a problem, and -1 is treated as true. Commented Nov 3, 2014 at 16:09
  • @Compass yes indeed i am using angular, this is within a controller, sorry for any confusion Commented Nov 3, 2014 at 16:10

1 Answer 1

2
if(data.applications.indexOf(index.id){ // this is missing a parenthesis

This line has the following actual behavior (thanks @Pointy for clarifying all the options)

  1. Not found (-1) = true
  2. First Element (0) = false
  3. Any other element (1 to n) = true

From your question, your expected output is:

  1. Not found (-1) = false
  2. Found (0 to n) = true

If you're attempting to use JS' 0 = false, anything else is true, then you can do:

angular.forEach($scope.applicationsHere, function(index) {
                    if(data.applications.indexOf(index.id) + 1) {
                        index.selected = true;
                    }
                });

Or, even shorter:

angular.forEach($scope.applicationsHere, function(index) {
                        index.selected = (data.applications.indexOf(index.id) + 1);
                });

That being said, I would still recommend doing an actual >= 0 check for indexOf. Coercing like this causes confusion for other people reading the code since you're using an index for a boolean output. You can use a ternary operator if you're looking for compactness too.

angular.forEach($scope.applicationsHere, function(index) {
                        index.selected = data.applications.indexOf(index.id) >= 0 ? true : false;
                });
Sign up to request clarification or add additional context in comments.

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.