Objects Lesson
Lesson Objectives
After this lesson, students will be able to:
Explain the difference between arrays and objects
Store key-value pairs inside an object
Access values by key-name
Add Object Properties
Change Object Properties
Explain why we use an object instead of an array
Manipulate objects and arrays declared as
const
List common errors made with objects
Use object properties with conditionals
Explain the difference between arrays and objects
We have seen the following datatypes:
String
Number
Boolean
Arrays
Arrays are a data structure. We use them to organize our data: in the case of arrays, we can organize our data into a sequential list data structure.
We can use arrays to store multiple pieces of data as a sequential list:
Each element has a corresponding index (or place), in sequence.
But with the array above, we don't know what the values mean. Does "blue" refer to the color of the vehicle? To the mood of the owner? Is it the model of the vehicle?
An object is also a data structure, but we can use objects to store data with greater specificity.
In JavaScript, objects are what we use to represent key-value pairs.
Store key-value pairs inside an object
Key-value pair syntax:
Unlike arrays, objects use named keys rather than ordered indexes. Each piece of data is bound to its key, rather than assigned an index. The data is not sequential.
In Javascript, an object is a way to group many pairs of keys and values together
We can console.log the entire object:
Access values by key-name
We can access the values stored in key using dot notation:
Question: Ask (5 min)
What differences do you see between the
vehicle
array and thecar
object syntax?
Differences between arrays and objects
Arrays are declared using the square brackets
var arr = [];
Objects are declared using the curly braces
var obj = {}
Objects contain key-value pairs. They are are the properties of the object
A key is like an index in an array, but it has
a name
it is unique
A key is really a string but we can omit the quotes.
A value is what a key refers to, and can be any datatype.
Class Exercise: Build an Object
Build an object in detail, demonstrating that:
We use a colon to separate the key and the value
We do not put semicolons after our values.
We separate our key-value pairs with a comma
Investigate
What properties should we add to the person object.
How can we access these properties.
Activity (10 min)
Dog
Create an object called
dog
that has the following properties:name (a string, give your dog a name)
age (a number, give your dog an age)
Remember the correct use of curly braces, colons, and commas! No semicolons!
Console.log the
dog
object to check if it's correctConsole.log just the dog's name
Console.log just the dog's age
Celebrity
Create an object called
celebrity
that has the following properties:name (a string, give the celebrity a name)
age (a number, give the celebrity an age)
isCurrrentlyTweeting (a boolean)
Console.log the
celebrity
objectConsole.log just the name of the
celebrity
Console.log just the age of the
celebrity
Console.log whether or not the
celebrity
is currently tweetingWrite conditional that will print "Turn off Twitter" if the celebrity is currently tweeting.
Add Object Properties
You can easily add more properties to a previously declared object:
=> { doors: 9 }
Add properties to the house
object by simply adding a key using dot notation and the value using an equals =
. Our house has no windows. Let's add some in without writing them straight into the object:
When we do it this way, the windows
key is added to the object.
=> { doors: 9, windows: 30 }
Add another property hasGarden
:
Change Object Properties
Changing the value of an existing key has the same syntax as creating a new key-value pair:
Activity (7 min)
Create an empty object called
macros
Do not write in to this empty object. Instead, add keys and values with macros.keyName = "value"
After each key-value addition, console.log the macros
object to make sure the new keys and values show up.
Add to the object a key
protein
with a value 'tempeh'Add to the object a key
carbohydrates
with a value 'spuds'Add to the object a key
fats
with a value 'olive oil'Console.log the
macros
object to check if all the macros are there
Activity: 10 minutes
Create an object called
guitar
with the following properties:a key
strings
with value 6a key
isAcoustic
with value true (boolean)
Change the value of
strings
to 100console.log the value of
strings
(guitar.strings
)
Change the value of
isAcoustic
to falseConsole.log the value of
isAcoustic
Without writing into your object directly, add a key
belongsTo
with the value 'Dimebag Darrell'Console.log the value of
belongsTo
Change the value of
belongsTo
to 'Mr. Rogers'Console.log the entire
guitar
object
Explain why we use an object instead of an array
When designing your programs, it is up to you to choose how to model your data. We can represent real-life things with our datatypes, but it's a matter of choosing the appropriate datatypes.
If the thing you want to model is just a list, use an array.
If the thing want to model has properties, use an object.
Using what we know about datatypes so far, which datatype would we use to model:
The name of your cat
The age of your cat
Your cat's favorite things
Whether your cat can speak French
Whether your cat can solve a Rubik's cube
Your cat
Manipulate objects and arrays declared as const
const
const
only prevents you from reassigning a variable, it doesn't prevent you from adding or changing elements of arrays or properties of objects.
You can do this:
Cannot do this:
Object literal shorthand
If variable names outside the object will correspond to the keys in the object, you can write shorthand like this for the mogwai
object:
=> { name: 'Gizmo', age: 1 }
This is equivalent to the longhand:
=> { name: 'Gizmo', age: 1 }
List common errors made with objects
Unique Keys
It just makes sense that keys ought to be unique within an object. Values, however, can be whatever.
An object can not have more than one key with the same name. If it does, the value will default to the last key with the same name, and the prior properties will be excluded on creation.
Conclusion: keys should be unique within an object. Values, however, are not unique.
Accessing and Naming Keys Using Brackets and Quotes
You can create and access any key with square brackets and quotes.
With square brackets and quotes, you can make key names with spaces and special characters, because the key is coerced into a string. But you then have to access the value from here on out with square brackets and quotes.
You would need also to access that key with the square brackets and quotes:
You could not access that key using dot notation.
Square brackets are nice if you need to programmatically generate a key name:
Keys That Are Numbers
If a key is just a number, that number will be coerced into a string, which is fine.
But, you cannot access, add, or change numbered keys with dot notation.
There is another way to access key-values using square brackets and quotes obj['1']
Activity
Create an empty object called
testObject
Give
testObject
a key called 'this is a test' with the value "test"Console.log the value of the key 'this is a test'
Give test object a key called
2
with the value "I'm just messing around with objects"Console.log the value of the key
2
Use object properties with conditionals
You can use object properties with conditionals, loops, etc
You can test to see if a property exists on an object:
This is because accessing a property that doesn't exist on an object gives you undefined
which is treated as false
.
Last updated
Was this helpful?