Objects (dictionaries) Flashcards
1
Q
Create an object
A
let obj = { key1: “value1”, key2: “value2” }
2
Q
Access value by key
A
obj.key1
obj[“key1”]
3
Q
Add or update key value pair
A
obj.key3 = “value3”
4
Q
Delete key value pair
A
delete obj.key1
5
Q
Check if key exists in object
A
‘key1’ in obj
6
Q
Get array of keys
A
Object.keys(obj)
7
Q
Get array of values from object
A
Object.values(obj)
8
Q
Loop through object
A
for (let key in obj) {
console.log(key)
}