JavaScript push to array JavaScript push to array json json

JavaScript push to array


It's not an array.

var json = {"cool":"34.33","alsocool":"45454"};json.coolness = 34.33;

or

var json = {"cool":"34.33","alsocool":"45454"};json['coolness'] = 34.33;

you could do it as an array, but it would be a different syntax (and this is almost certainly not what you want)

var json = [{"cool":"34.33"},{"alsocool":"45454"}];json.push({"coolness":"34.33"});

Note that this variable name is highly misleading, as there is no JSON here. I would name it something else.


var array = new Array(); // or the shortcut: = []array.push ( {"cool":"34.33","also cool":"45454"} );array.push (  {"cool":"34.39","also cool":"45459"} );

Your variable is a javascript object {} not an array [].

You could do:

var o = {}; // or the longer form: = new Object()o.SomeNewProperty = "something";o["SomeNewProperty"] = "something";

and

var o = { SomeNewProperty: "something" };var o2 = { "SomeNewProperty": "something" };

Later, you add those objects to your array: array.push (o, o2);

Also JSON is simply a string representation of a javascript object, thus:

var json = '{"cool":"34.33","alsocool":"45454"}'; // is JSONvar o = JSON.parse(json); // is a javascript objectjson = JSON.stringify(o); // is JSON again


That is an object, not an array. So you would do:

var json = { cool: 34.33, alsocool: 45454 };json.supercool = 3.14159;console.dir(json);