-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_object_keys.js
More file actions
40 lines (32 loc) · 821 Bytes
/
dynamic_object_keys.js
File metadata and controls
40 lines (32 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Accessing object properties using "." dot notation
const object1 = {
key1: "value1",
};
console.log(object1.key1);
// Accessing object properties using "[]" square bracket notation
const object2 = {
key2: "value2",
};
console.log(object2["key2"]);
// Set variable as key for object
const variable = "key3";
const object3 = {
[variable]: "value3",
key5: "",
key6: "value6",
};
console.log(object3);
console.log(object3.key3);
// Dynamically updating object key
function updatingObject(key, value) {
object3[key] = value;
}
// Adding key
updatingObject("key4", "value4");
console.log(object3);
// Updating existing key in the object
updatingObject("key5", "value5");
console.log(object3);
// Changing existing key's value in the object
updatingObject("key6", "updated value6");
console.log(object3);