javascript - Change code base on variable value -


this question has answer here:

i have following javascript variable:

scope.model = {   errors: {     email: ["error1", "error2"],     name: ["error"]   } } 

and have function follows:

function (scope, element, attributes) {               if (scope.model.errors) {     if (scope.model.errors[attributes.validator])        // } 

the problem errors not on same scope variable.

i can have like:

scope.view = {   newerrors: {     email: ["error1", "error2"],     name: ["error"]   } 

inside function know how variable are:

function (scope, element, attributes) {                var errors = attributes["validatorerrors"];    // note: errors case "view.newerrors"   // following code become:     if (scope.view.newerrors) {     if (scope.view.newerrors[attributes.validator])        // } 

update

i have tried [] before understand why not working:

function (scope, element, attributes) {                var errors = attributes["validatorerrors"];    if (scope[errors]) {     if (scope.[errors][attributes.validator])        // } 

if errors = 'errors' work ...

if errors = 'model.errors' won't. in case need:

scope['model']['errors'] ... 

how can solve this?

use array notation:

if (scope[errors]) {   if (scope[errors][attributes.validator]) {     // 

Comments