-
-
Save sandrabosk/cb07e67b4587ac7ed3485b1c7dd4fbcd to your computer and use it in GitHub Desktop.
Course source code: https://codistwa.com/guides/data-structures-objects-dictionaries/. More courses on https://codistwa.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ============================================================ | |
// Definition of an object / dictionary | |
// ============================================================ | |
const fruits = { | |
color: 'red', | |
length: 3 | |
} | |
// ============================================================ | |
// Copy by reference | |
// ============================================================ | |
const fruits = { | |
color: 'red', | |
length: 3 | |
} | |
const basket = fruits; | |
// point to the same object | |
console.log(basket.color); | |
basket.color = 'blue'; | |
console.log(fruits.color); // blue | |
// changes | |
console.log(basket.color); | |
console.log(basket === fruits); // true | |
// ============================================================ | |
// Delete a value key pair | |
// ============================================================ | |
const basket = { | |
color: 'red', | |
length: 3 | |
} | |
delete basket.color; | |
console.log(basket); // { length: 3 } | |
// ============================================================ | |
// Add a value key pair | |
// ============================================================ | |
const basket = { | |
color: 'red', | |
length: 3 | |
} | |
basket.size = 'tall'; | |
console.log(basket); // { color: 'red', length: 3, size: 'tall' } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ============================================================ | |
# Definition of an object / dictionary | |
# ============================================================ | |
fruits = { | |
'color': 'red', | |
'length': 3 | |
} | |
# ============================================================ | |
# Copy by reference | |
# ============================================================ | |
fruits = { | |
'color': 'red', | |
'length': 3 | |
} | |
basket = fruits | |
# point to the same object | |
print(basket['color']) | |
basket['color'] = 'blue' | |
print(fruits['color']) # blue | |
# changes | |
print(basket['color']) | |
print(basket == fruits) # True | |
# ============================================================ | |
# Delete a value key pair | |
# ============================================================ | |
basket = { | |
'color': 'red', | |
'length': 3 | |
} | |
del basket['color'] | |
print(basket) # {'length': 3} | |
# ============================================================ | |
# Add a value key pair | |
# ============================================================ | |
basket = { | |
'color': 'red', | |
'length': 3 | |
} | |
basket['size'] = 'tall' | |
basket.update({'size': 'tail'}) | |
print(basket) # {'color': 'red', 'length': 3, 'size': 'tail'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment