Matthew C.
—You want to add a key-value pair to a JavaScript object, how do you do this?
We’ll describe six ways to add a key-value pair to a JavaScript object.
Property accessors use dot notation or bracket notation to give access to an object’s properties, which can also be called keys. You can use them to add a key-value pair to an object. It’s the most straightforward way of adding a key-value pair to an object.
Using dot notation is another common way to add a key-value pair to an object.
const obj = { name: "Ben" }; obj.age = 22; console.log(obj); // { name: 'Ben', age: 22 }
The key should be a String
or a symbol
. If you need a key to be a different primitive value or an object, or if the insertion order of the key-value pairs is important, don’t use a regular JavaScript object. Use a Map
.
Dot notation does have some limitations. If the key is dynamic, if it contains spaces or hyphens, or if it starts with a number you’ll get an error message. In these cases, you can use bracket notation:
const obj = { name: "Ben" }; const prop1 = "prop name"; const prop2 = "prop-name"; const prop3 = "1prop-name"; obj[prop1] = "value 1"; obj[prop2] = "value 2"; obj[prop3] = "value 3"; console.log(obj); // {"name": "Ben", "prop name": "value 1", "prop-name": "value 2", "1prop-name": "value 3"}
spread (...)
SyntaxYou can use the spread (...)
syntax to add a key-value pair to an object:
const obj = { name: "Ben" }; const newObj = { ...obj, age: 22 }; console.log(newObj); // { name: 'Ben', age: 22 }
You can spread in any iterable, such as an array, object, or String. Unlike the other methods explained here, spread (...)
syntax does not mutate the original object, which may be useful if you don’t want to change the original object. It creates a new object, which makes its performance the worst of the methods explained here.
You can add multiple properties at once or merge multiple iterables, such as objects. The merge is shallow: Any nested objects will not be copied to the new object.
If a property is added that already exists on the object, the property value will be overwritten:
const obj = { name: "Ben", age: 22 }; const obj2 = { ...obj, age: 33 }; console.log(obj2); // { name: 'Ben', age: 33 }
Object.assign()
The Object.assign()
method is similar to using the spread syntax, but it only joins two objects and mutates the target object.
const obj = { name: "Ben" }; Object.assign(obj, { age: 22 }); console.log(obj); // { name: 'Ben', age: 22 }
The first argument is the target object; the second object is the source object. The source object’s properties are added to the target object.
Object.defineProperty()
The Object.defineProperty()
method can be used to add or modify a property on an object. This method is useful for more complex property additions where you want to set property descriptors such as enumerable
, configurable
, and writable
.
const obj = { name: "Ben" }; Object.defineProperty(obj, "age", { value: 22, writable: false, enumerable: true, configurable: true, }); console.log(obj); // { name: 'Ben', age: 22 } obj.age = 24; // this won't change the 'age' key because 'writable' is set to false console.log(obj); // { name: 'Ben', age: 22 }
If you’re looking to get a deeper understanding of how JavaScript application monitoring works, take a look at the following articles:
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at [email protected].
If you are a California resident, see our Supplemental notice.