How do I push new values to the following javascript array?

json = {"cool":"34.33","alsocool":"45454"}

I tried


but it didnt work

Recommended Answers

All 2 Replies

you could jQuery's extend method.

var obj1 = {"cool":"34.33","alsocool":"45454"};
var obj2 = {"coolness":"34.33"};
$.extend(obj1, obj2);

The value of the first object will contain obj2.


The above statement will log something like this:

{"cool":"34.33","alsocool":"45454", "coolness":"34.33"}

If you don't want to use jQuery, you can try the native and easy way by doing this:

json.coolness = "34.33";

Everything in JavaScript is an object, you can assign properties to objects easily and set it's value

Sir Saula,

{"cool":"34.33","alsocool":"45454"} is a plain javascript object not an array.

Add extra properties as per Lambing's suggestions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.