2

can i use string variable in angular.forEach() loop like this:

var variable= "keyname";
angular.forEach($scope.arrCategory[0].variable,function(k,d){
    //alert(k);
    angular.forEach(k,function(kk, dd){
        alert(dd);
    });
});  

if it is wrong how should i use this variable in loop?

1 Answer 1

3

You should use bracket notation to access property by variable name:

var variable = "keyname";
angular.forEach($scope.arrCategory[0][variable], function(k,d) {
    angular.forEach(k, function(kk, dd) {
        alert(dd);
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

SyntaxError: missing name after . operator angular.forEach($scope.arrCategory[0].["static"],function(k,d){........ : this was the console message when handle it in your way
@thathsara you forgot to remove the dot before the opening bracket
thanx. it works.actually a have done a really big mistake @ JB Nizet.

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.