1

I have html like this :

<td><span ng-if="isuser(user) == false" type="button" >not allowed</span> </td>

the above html is displayed only if the function isuser(user) return false.

I have an array like this :

$scope.list = ["456", "111", "459"];

Now the user object has an array called id. This id array contains a number key whose value can be 456,111 or 459 and sometimes it may be empty also.

The user object :

{"name":"pravin","status":"Live","id":[{"number":"111"}],"msg":"test"}

Here is the isuser function :

$scope.isuser = function(user) {
  for (var i = 0; i < $scope.list.length; i++){
  var exists = user.id.find(({number}) => number === $scope.list[i]);
  }
if (exists)
  return true;
else
  return false;
};

but this always returns false.

I want the for loop to check if none of the values of the number key exists in the list array and then only return false. How do I do it?

3
  • post user object also Commented Jan 14, 2019 at 7:52
  • Please post user.id array. Commented Jan 14, 2019 at 7:52
  • posted. check the updated post Commented Jan 14, 2019 at 7:55

2 Answers 2

2

The issue is that the below statement

var exists = user.id.find(({number}) => number === $scope.list[i]);

updates every time the exists variable and always you'll get the last change.

One approach could be using some method by passing a callback function as argument.

$scope.isuser = function(user) {
   var exists = user.id.some(({number}) => $scope.list.includes(number));
   return exists;
}
Sign up to request clarification or add additional context in comments.

2 Comments

this worked. your answer is minimal and very efficient
please take a look at my question here
1

Try this it will help you

  $scope.isuser = function(user) {
       var exists = false;
    for (var i = 0; i < $scope.list.length; i++){
      exists = user.id.find(({number}) => number === $scope.list[i]);
    }
     if(i==$scope.list.length-1){
      if (exists)
        return true;
      else
        return false;
      }
  loop };

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.