Member-only story
To continue the data structure series I have going on, I’ll be talking about objects in this post.
My previous post on arrays can be found here.
Objects are another data structure that has a key and value pair. Object data cam be accessed through properties. Using objects as a data structure is faster than arrays when looking up data.
let cat = {
name: 'Bella',
color: 'orange',
personality: 'fiesty',
friends: 'Cookie',
enemies: ['Acrobat', 'Daemon', 'Barack']
}//To access the friend data inside the cat object, you can use dot notation:cat.friends // 'Cookie'To access enemies data inside an object that has an array, you can use bracket notation :cat.enemies[1] // 'Daemon'
You can also add properties inside an object:
let dog = {
name: 'Jack',
}dog.friends = 'Bella'//Now when you call the dog object, the new object looks like this:let dog = {
name: 'Jack',
friends: 'Bella'
}
Keys can also be strings. If the string is one word, you can access it with dot notation, but if the string is two words you access it with bracket notation:
let animals = {
'cats': ['Bella', 'Cookie'],
'dogs': 'Jack',
'pet roommates': ['Jack', 'Bella']
}//To access the 'pet-roommates'…