Tushar Jangade
Tushar Jangade

Reputation: 25

javascript alert prints Object Object

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

Upvotes: 0

Views: 122

Answers (3)

Alessio Cantarella
Alessio Cantarella

Reputation: 5201

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(', '));

Upvotes: 1

Nikhil Goyal
Nikhil Goyal

Reputation: 1973

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.

Upvotes: 2

Mayank
Mayank

Reputation: 158

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

Upvotes: 1

Related Questions