Table of Contents
Using Bracket Notation
We can use bracket notation to update the key with the new value in Python.
Update Single Key with New Value
To update a single key with a new value, use bracket notation to read it and update it with a new value.
1 2 3 4 5 6 7 8 9 10 11 |
let user = { "name": "Anonymous", "email": "@gmail.com", "age": 36, "city": "London", }; console.log(user); user['age']+=1; console.log(user); |
1 2 3 4 |
{ name: 'Anonymous', email: '@gmail.com', age: 36, city: 'London' } { name: 'Anonymous', email: '@gmail.com', age: 37, city: 'London' } |
Bracket notation in JavaScript is a way to access properties of an object or elements within an array using square brackets using the object[property]
syntax.
Property names must be valid JavaScript identifiers enclosed in quotes to access object properties. Array indices must be integers to access array elements. It is similar to dot notation but more flexible because developers can use strings, variables, and expressions within the brackets.
Bracket notation also updates the values of the property. For example, we used bracket notation to access the age
of the array user
and incremented it by 1
using user["age"]+=1
.
Update Multiple Keys with New Values
To update multiple keys with new values, use bracket notation to access the elements and update them with new values.
1 2 3 4 5 6 7 8 9 10 11 12 |
let user = { "name": "Anonymous", "email": "@gmail.com", "age": 36, "city": "London", }; console.log(user); user['age']+=1; user["city"] = "Paris" console.log(user); |
1 2 3 4 |
{ name: 'Anonymous', email: '@gmail.com', age: 36, city: 'London' } { name: 'Anonymous', email: '@gmail.com', age: 37, city: 'Paris' } |
We already discussed bracket notation while explaining the code snippet for updating a single key using bracket notation. In this section, we used it to update multiple keys with new values. For example, we updated the age
and city
of the user
.
Using Dot Notation
We use dot notation to update the key with the new value in Python.
Update Single Key with New Value
To update a single key with a new value, use dot notation to read the element and update it with a new value.
1 2 3 4 5 6 7 8 9 10 11 |
let user = { "name": "Anonymous", "email": "@gmail.com", "age": 36, "city": "London", }; console.log(user); user.age+=1; console.log(user); |
1 2 3 4 |
{ name: 'Anonymous', email: '@gmail.com', age: 36, city: 'London' } { name: 'Anonymous', email: '@gmail.com', age: 37, city: 'London' } |
Dot notation in JavaScript is a way to access object properties and methods directly by name. It is more straightforward, more readable, and more efficient than bracket notation.
It uses a period (.
) to separate the object
and its property
. So the syntax looks like this: object.property
. It is also great for updating property values quickly and easily.
For example, we used dot notation to access the age
of the array user
and incremented it by 1
using user.age+=1
.
Update Multiple Keys with New Values
To update multiple keys with new values, use dot notation to access the elements and update them with new values.
1 2 3 4 5 6 7 8 9 10 11 12 |
let user = { "name": "Anonymous", "email": "@gmail.com", "age": 36, "city": "London", }; console.log(user); user.age+=1; user.city = "Paris" console.log(user); |
1 2 3 4 |
{ name: 'Anonymous', email: '@gmail.com', age: 36, city: 'London' } { name: 'Anonymous', email: '@gmail.com', age: 37, city: 'Paris' } |
We discussed dot notation while explaining the code snippet for updating a single key using dot notation. In this section, we used it to update multiple keys with new values. For example, we updated the age
and city
of the user
.
We have learned how to update single/multiple keys with new values in JavaScript. Now, think of a project where we have to update all keys with new values; let’s learn how to do that.
Using forEach()
Method
Update All Keys with New Values
To update all keys with new values using the forEach()
method:
- Use the
Object.keys()
method to create an array of keys of the object. - Use the
forEach()
method to iterate over the array and update its values.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
let user = { "name": "Anonymous", "email": "@gmail.com", "age": 36, "city": "London", }; console.log(user); Object.keys(user).forEach((key) => { user[key] = ''; }); console.log(user); |
1 2 3 4 |
{ name: 'Anonymous', email: '@gmail.com', age: 36, city: 'London' } { name: '', email: '', age: '', city: '' } |
The Object.keys() method in JavaScript retrieves the names of all the enumerable properties of an object so that we can access and manipulate their values.
It returns an array of strings containing the names of all properties of the specified object. It is more efficient than looping through the entire object. For example, we used this method to extract the keys from the user
.
JavaScript’s forEach() method allows developers to iterate over an array or an object and execute a provided function once for each element. For example, we can use this method to add elements to an array, modify existing elements, or count elements.
For example, we applied this method to the array of keys of the user
to update the values with the empty strings.
Using map()
Method
Update All Keys with New Values
To update all keys with new values using the map()
method:
- Use the
Object.keys()
method to create an array of keys of the object. - Use the
map()
method to iterate over the array and update its values.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
let user = { "name": "Anonymous", "email": "@gmail.com", "age": 36, "city": "London", }; console.log(user); Object.keys(user).map((key) => { user[key] = ''; }); console.log(user); |
1 2 3 4 |
{ name: 'Anonymous', email: '@gmail.com', age: 36, city: 'London' } { name: '', email: '', age: '', city: '' } |
We have already discussed the Object.keys()
method while explaining the code snippet for using the forEach()
method.
In this section, we used the map() function that processes each element of the array and executes the provided function on them. For example, we applied this method to the array of keys created with the Object.keys()
method to update the values with the empty strings.