简体   繁体   中英

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM