How do I push new values to the following javascript array?
json = {"cool":"34.33","alsocool":"45454"}
I tried
json.push("coolness":"34.33");
but it didnt work
How do I push new values to the following javascript array?
json = {"cool":"34.33","alsocool":"45454"}
I tried
json.push("coolness":"34.33");
but it didnt work
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.
console.log(obj1);
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.