简体   繁体   中英

Objects in array display as [object Object]

I'm trying to save an array which includes a object to a hidden field in a form . When I do so, the object doesn't show up, but is display like "[object Object]" instead.

arrayObject = [
    "something",
    { objectKey: "something else", otherObjectKey: "totally different" },
    "quite similar"
];

$("input").val(arrayObject);


This outputs

"something,[object Object],quite similar"

What can I do different?

Set it into the hidden field after serializing it as JSON like this:

$("input").val(JSON.stringify(arrayObject));

And parse it into the arrayObject while reading it back like this:

var savedArray = JSON.parse($("input").val());

$("input").val(JSON.stringify(arrayObject))

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