0

I want to access the property nombreprofesional, noting that I do not know what the property is called. I want to avoid doing myVar.errors["nombreprofesional"]. How would he do it?

var myVar={
  "errors": {
   "nombreprofesional": [
   "El contenido ingresado en nombreprofesional es inválido."
   ]
  }
}
yavg
  • 2,761
  • 7
  • 45
  • 115
  • @benvc `Object.prototype.keys` is not a thing. You probably mean `Object.keys(myVar)`. – deceze Aug 02 '18 at 15:28
  • Thanks @deceze for correcting my initial erroneous syntax (deleted comment so as not to confuse future readers). – benvc Aug 02 '18 at 15:36

1 Answers1

1

You could do something like

var key=Object.keys(myVar.errors)[0];
myVar.errors[key];

If you know that there is only ever one key in the error object. Otherwise you could loop through all the keys

for (var k in myVar.errors){
    if (!myVar.errors.hasWonProperty(k)){continue;}
    myVar.errors[k];
}
sbrass
  • 905
  • 7
  • 12