0

How to add new attribute to JSON object using JavaScript?

There are several techniques to add a property to an existing object in JavaScript:

object["property"] = value

var obj = {
  	"attr1": "value1",
  	"attr2": "value2"
};

obj["attr3"] = "value3";

console.log(obj);
RUN

object.property = value;

var obj = {
  	"attr1": "value1",
  	"attr2": "value2"
};

obj.attr3 = "value3";

console.log(obj);
RUN

In ECMAScript 2015 you can use Spread Syntax to extend object with new attributes- three dots {...}

var obj = {
  	"attr1": "value1",
  	"attr2": "value2"
};

obj = {...obj, attr3: "value3"};

console.log(obj);
RUN
add comment
Suggest a different solution or add another example