1

My API returns validation erros with a object like:

{
"message": {
    "cnpj": [
        "The cnpj field is required."
    ],
    "nome": [
        "The nome field is required."
    ]
}

}

How i can map each object for a console.log() or li method using pure vanilla JS?

Had tried using error.response.data.message but no success, because message is an object.

1 Answer 1

1

try:

Object.entries(error.response.data.message)
  .map(([, fieldErrors]) => 
    fieldErrors.map(fieldError => <li>{fieldError}</li>)
  )

Object.entries convert an object to an array containing [key, value] for every key in an object, meaning:

let obj = {a: 1, b: 2}
console.log(Object.entries(obj)) // will become [['a', 1], ['b', 2]]
Sign up to request clarification or add additional context in comments.

Comments

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.