Reputation: 714
I have a JS Array of Objects as follows:
var tables = new Array();
var table = new Object();
table.no=1001;
table.name="Table-1";
tables.push(table);
table.no=1001;
table.name="Table-1";
tables.push(table);
Now I need to send this tables
object-array through a hidden field in a form. Can I directly assign the value of hidden field as tables
object.
Upvotes: 0
Views: 2832
Reputation: 27855
use JSON.stringify() to convert it to JSON string and send your data.
There is an issue in the way you are creating an appending objects. Each time you are editing the same object. It will result in all the elemnts in your array being the last object you made as they are all references to the same object you have. resolve this by creating new objects when you append items to the array
var tables = new Array();
tables.push({"no":1000,"name":"Table-0"});
tables.push({"no":1001,"name":"Table-1"});
alert(JSON.stringify(tables));
Upvotes: 3
Reputation: 2351
You could stringify the value, put it in the field, send it to the server and then decode it. To stringify an object:
JSON.stringify(tables);
Assuming that you're using PHP on the server, you could decode it using json_decode
function.
Upvotes: 0