0

This is my obj, I just want to loop through this and show the errors:

var obj = {
 "error": {
 "errors": {
 "username": {
 "properties": {
 "message": "username field is empty.",
 "type": "required",
 "path": "username"
 },
 "kind": "required",
 "path": "username"
 },
 "email": {
 "properties": {
 "message": "email field is empty.",
 "type": "required",
 "path": "email"
 },
 "kind": "required",
 "path": "email"
 },
 "password": {
 "properties": {
 "message": "password field is empty.",
 "type": "required",
 "path": "password"
 },
 "kind": "required",
 "path": "password"
 }
 },
 "_message": "User validation failed",
 "message": "User validation failed: username: username field is empty., email: email field is empty., password: password field is empty."
 }
}

I want to show the errors: properties.message but I'm having hard time, this is what I tried so far:

for (var key in obj.error.errors) {
 for (var key2 in key.properties){
 for (var key3 in key2.message){
 console.log(key3)
 } 
 } 
}

But the console is blank.

asked Jul 22, 2020 at 6:15
1
  • something looks wrong with your 2nd and 3rd for loop :) Commented Jul 22, 2020 at 6:18

3 Answers 3

1

In the loop you are getting the key, not the object itself. Also you don't need to add all those loops if your data is structured like that. Just pick the errors, iterate through all.

for(var d in obj.error.errors){
 console.log(a[d].properties.message)
}
answered Jul 22, 2020 at 6:23

Comments

0

a working solution

Object.values(obj.error.errors).forEach((error) => { console.log(error.properties.message) })

Looking to your data structure, you only need to iterate on the 'errors' list

answered Jul 22, 2020 at 6:20

Comments

0

Check this out, it will return you in the properties:message format

const obj = {
 "error": {
 "errors": {
 "username": {
 "properties": {
 "message": "username field is empty.",
 "type": "required",
 "path": "username"
 },
 "kind": "required",
 "path": "username"
 },
 "email": {
 "properties": {
 "message": "email field is empty.",
 "type": "required",
 "path": "email"
 },
 "kind": "required",
 "path": "email"
 },
 "password": {
 "properties": {
 "message": "password field is empty.",
 "type": "required",
 "path": "password"
 },
 "kind": "required",
 "path": "password"
 }
 },
 "_message": "User validation failed",
 "message": "User validation failed: username: username field is empty., email: email field is empty., password: password field is empty."
 }
};
const objVal = Object.values(obj.error.errors);
const errors = objVal.reduce((obj, item) => {
 return (obj[item.path] = item.properties.message, obj)
},{})
console.log(errors);

answered Jul 22, 2020 at 6:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.