0
$scope.ptArray={m1 : $scope.somevalue1, 
                m2 : $scope.somevalue2, 
                m3 : $scope.somevalue3};
$scope.errMsg={m1 : 'msg1',
                 m2 : 'msg2', 
                 m3 : 'msg3'};
if($scope.ptArray.this==""){
            alert($scope.errMsg.this);
            }

'this' doesn't work here. If I use m1, m2 or m3 instead of 'this' it will work, but only for that variable. What to use in the place of this

2
  • What do you expect "this" to be? It's not magic. Commented Aug 25, 2016 at 6:35
  • I was just trying to tell what i wanted sir :) Commented Aug 25, 2016 at 8:11

3 Answers 3

2

You can do something like this:

Get all the keys that have an empty String as a value:

var empty = [];
Object.keys($scope.ptArray).forEach((k) => {if(ptArray[k] == ""){empty.push(k);}});

Create an alert with the messages for all those keys:

var msg = "";
empty.forEach((k) => {msg+=$scope.errMsg[k]+"\n"});
if(msg.length > 0){
    alert(msg);
}

or do all that at once in a single loop:

var msg = "":
Object.keys($scope.ptArray).forEach((k) => 
      {
        if(ptArray[k] == ""){
            msg += $scope.errMsg[k]+"\n";
        }
      });
if(msg.length > 0){
    alert(msg);
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to access to the dict you can use...

if($scope.ptArray[str]==""){
     alert($scope.errMsg[str]);
}

Where str is a String with value 'm1', 'm2' or 'm3'. But Use an array can be a better option:

  if($scope.ptArray[index]==""){
     alert($scope.errMsg[index]);
}

where index is an integer value.

I don't know what you want to do with this.

1 Comment

$scope.errMsg[index] returns [object Object] doesn't work
0

Can try this

$scope.ptArray={m1 : $scope.somevalue1, 
            m2 : $scope.somevalue2, 
            m3 : $scope.somevalue3};
$scope.errMsg={m1 : 'msg1',
             m2 : 'msg2', 
             m3 : 'msg3'};

Object.keys($scope.ptArray).forEach(function(node){
           if($scope.ptArray[node]==""){
                console.log($scope.errMsg[node]);
           }
     });

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.