4
I have the following object in javascript:
{
"tipo_entrega_id" : ["Valor não é válido"],
"outro_campo" : ["valor deve ser preenchido"]
}
The values are not defining by me, they come dynamically through a response to an ajax request.
I always need to take the first value of this object (without knowing what the key is) dynamically.
How do I do this in Javascript?
you refer to first as who says "any"? or you expect those properties to have an order?
– Sergio
"". Because PHP gives me an error in json, as I described above. Then I just want to display the first one (any first one) of the list
– Wallace Maxters
Hmmm... that doesn’t make much sense, to give any, cross-browser, you can do like this: https://jsfiddle.net/ov5ym27L/ but if you don’t know what the first thing is that it’s useful to debug?
– Sergio
@Sergio is because if the request returns an error, it returns the same as the object I showed above. Any error that displays to me is fine! If all goes well, there are no mistakes, then my ajax "Success" runs smoothly
– Wallace Maxters
"if the request returns an error, it returns equal to the object I showed above" - it is the server, the browser or a JS library?
– Sergio
@Sergio I use Laravel. Laravel returns a JSON (status 422). From there on
error
from the ajax I capture theerror.responseJSON
. It comes with the above object, but I only need one error message, not all. As the error is already automatic of the system, instead of changing everything that is already ready, I preferred to do so in this part.– Wallace Maxters
And why not choose one of the keys if you already know the structure of the object, instead of "drawing" a key?
– Sergio
@Sergio because one time can come a key, another time can come another. For example: If I have the fields "tipo_id" and "note", if I fill in "tipo_id" and forget "note, it will return the key "note" and the errors in a
Array
. If it’s the other way around, you can see "tipo_id". If you stop filling both, then both will come. It’s a validation.– Wallace Maxters