0

I have the following:

var a = {
 "message":"The request is invalid.",
 "modelState":{
    "model.ConfirmPassword":["The password and confirmation password do not match."]
  }
}

var keys = a.modelState.keys;
var firstError = a.modelState[keys[0]];

I would like to have the variable firstError set to "The password and confirmation password do not match."

However it is giving me "Cannot read property '0' of undefined".

0

2 Answers 2

2

There's no "first" key as keys in objects aren't ordered.

If you want any one, you can do this :

var anyError = a.modelState[Object.keys(a.modelState)[0]];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much. Atually I do want "any one" so your answer is perfect for me!
0

You would probably need this?

var firstError = a.modelState["model.ConfirmPassword"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.