And we want to add prop2 : 2 to this object, these are the most convenient options:
Dot operator: object.prop2 = 2;
square brackets: object['prop2'] = 2;
So which one do we use then?
The dot operator is more clean syntax and should be used as a default (imo). However, the dot operator is not capable of adding dynamic keys to an object, which can be very useful in some cases. Here is an example:
const obj = {
prop1: 1
}
const key = Math.random() > 0.5 ? 'key1' : 'key2';
obj[key] = 'this value has a dynamic key';
console.log(obj);
Merging objects:
When we want to merge the properties of 2 objects these are the most convenient options:
Object.assign(), takes a target object as an argument, and one or more source objects and will merge them together. For example:
The spread syntax is less verbose and has should be used as a default imo. Don't forgot to transpile this syntax to syntax which is supported by all browsers because it is relatively new.
Object.assign() is more dynamic because we have access to all objects which are passed in as arguments and can manipulate them before they get assigned to the new Object.
supported by most of browsers, and it checks if object key available or not you want to add, if available it overides existing key value and it not available it add key with value
example 1
let my_object = {};
// now i want to add something in it
my_object.red = "this is red color";
// { red : "this is red color"}
example 2
let my_object = { inside_object : { car : "maruti" }}
// now i want to add something inside object of my object
my_object.inside_object.plane = "JetKing";
// { inside_object : { car : "maruti" , plane : "JetKing"} }
example 3
let my_object = { inside_object : { name : "abhishek" }}
// now i want to add something inside object with new keys birth , gender
my_object.inside_object.birth = "8 Aug";
my_object.inside_object.gender = "Male";
// { inside_object :
// { name : "abhishek",
// birth : "8 Aug",
// gender : "Male"
// }
// }
Two most used ways already mentioned in most answers
obj.key3 = "value3";
obj["key3"] = "value3";
One more way to define a property is using Object.defineProperty()
Object.defineProperty(obj, 'key3', {
value: "value3", // undefined by default
enumerable: true, // false by default
configurable: true, // false by default
writable: true // false by default
});
This method is useful when you want to have more control while defining property.
Property defined can be set as enumerable, configurable and writable by user.
Simply adding properties:
And we want to add
prop2 : 2
to this object, these are the most convenient options:object.prop2 = 2;
object['prop2'] = 2;
So which one do we use then?
The dot operator is more clean syntax and should be used as a default (imo). However, the dot operator is not capable of adding dynamic keys to an object, which can be very useful in some cases. Here is an example:
Merging objects:
When we want to merge the properties of 2 objects these are the most convenient options:
Object.assign()
, takes a target object as an argument, and one or more source objects and will merge them together. For example:...
Which one do we use?
Object.assign()
is more dynamic because we have access to all objects which are passed in as arguments and can manipulate them before they get assigned to the new Object.In case you have multiple anonymous Object literals inside an Object and want to add another Object containing key/value pairs, do this:
Firebug' the Object:
returns:
Code:
will add
Object {name="Peanuts", value="12"}
to the Comicbook Objectsupported by most of browsers, and it checks if object key available or not you want to add, if available it overides existing key value and it not available it add key with value
example 1
example 2
example 3
We can do this in this way too.
You could use either of these (provided key3 is the acutal key you want to use)
or
If key3 is a variable, then you should do:
After this, requesting
arr.a_key
would return the value ofvalue3
, a literal3
.Two most used ways already mentioned in most answers
One more way to define a property is using Object.defineProperty()
This method is useful when you want to have more control while defining property. Property defined can be set as enumerable, configurable and writable by user.