0

this is my code which prints below output on the alert.

alert(JSON.stringify( args.errors,null,4));

this is the output.

{
    "Weight":{
        "errors":[
            "Hello My name is John"
        ]
    }
}

I just want "Hello My name is john" in the alert box. Any help will be great

  • 2
    Does this answer your question? [Print content of JavaScript object?](https://stackoverflow.com/questions/1625208/print-content-of-javascript-object) –  Jan 14 '20 at 19:44

3 Answers3

2

You can use args.Weight.errors.join(', ') to alert the errors.

In case if there is only one error, it will be shown. Otherwise, all the errors will be separated by ,.

I am assuming you are having

let args = {
    "Weight":{
        "errors":[
            "Hello My name is John"
        ]
    }
};

alert(args.Weight.errors.join(', '));

It should work.

Nikhil Goyal
  • 1,945
  • 1
  • 9
  • 17
1

I assume the output you mentioned is the error data and you want to display the errors from the that.

const data = {
    Weight: {
      errors: ["Hello My name is John"]
    }
};

To alert the users with above data you can use

alert(data.Weight.errors[0])

Test at snippet at CodeSandbox

Mayank
  • 148
  • 8
1

If args is your object, you could simply alert args.Weight.errors and, in case there is more than one error, add join(', ') to list them separated by ,.

let args = {
  "Weight": {
    "errors": [
      "Hello My name is John"
    ]
  }
};
alert(args.Weight.errors.join(', '));
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34