Homer Homer - 1 year ago 59
Javascript Question

Push objects to array and get the array content

I get results from Ajax call and want to push it to an array as objects;

This is my try:

var myList = [""];

url: 'list.json',
dataType: 'json',
success: function (data) {

for (var i = 0; i < data.length; i++) {
id: data[i].id,
text: data[i].text


The output I get is:

["",Object, Object, Object, ...]

I'm wondering how can I get the output like this:

["", {id:"id", text:"text"}, {id:"id", text:"text"}, ...]

Answer Source

You can use JSON.stringify() method to convert the object to JSON string.


UPDATE : Your question output is not a valid json, to convert into that format use String#replace method.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download