8. Objects
Table of Contents:
Summary
Objects are data structures that store multiple pieces of data as key-value pairs called properties, useful for representing real-world entities like users or products.
Object properties can be accessed using dot notation (
object.property) or bracket notation (object["property"]), with bracket notation required when using variables as keys.Objects are mutable, meaning you can add, modify, or delete properties after creation using assignment, the
deletekeyword, or direct property access.Objects can contain nested data including arrays and other objects, allowing for complex data structures.
Object shorthand allows you to create objects more concisely when variable names match property names.
Destructuring provides a convenient way to extract multiple properties from objects into separate variables.
The basics
Objects are a data type that can store multiple pieces of data as key:value pairs called properties
Objects are also referred to as dictionaries and hashmaps:
Objects are useful for storing collections of data related to a single "thing", like data about a user.
Objects can have arrays and other objects nested inside.
When creating an object, the quotes around the key name are optional, except when the key includes spaces or begins with a number:
Accessing Objects with Dot Notation and Bracket Notation
Object values can be accessed and/or mutated using dot notation or bracket notation
Dynamic Properties Challenge
When the key name of a property is stored in a variable, we must use bracket notation (we can't use dot notation).
Complete the program below so that it lets users add words to the dictionary!
Objects are Reference Types
We've already learned that arrays are reference types and so are objects. In fact, arrays are just a kind of object!
So, when we assign an object to a variable, we are storing a reference to the object's location in memory, not the values themselves.
And when we assign a variable holding an object to another variable, each variable holds a reference to the same object
We can also use the spread syntax (...) to copy the values of one object into a new object. This is particularly useful when creating pure functions:
Iterating Over Keys and Values of an Object
One of the key benefits of an Array is that we can easily iterate through its values with a for loop. This is possible because we can use the variable i to step through the indexes of the array.
We can't use a for loop with an object: it doesn't have countable indexes and their keys aren't in any particular order!
But we CAN turn the object into an array and then iterate through that array.
The Object.keys(obj) function puts all of the keys from the given object (obj) into a new array and returns it. We can then loop through those keys and use them to access their associated values:
Object.values(obj) does the same thing but instead puts the values of the given object (obj) into an array.
Advanced Object Syntax
Object Shorthand Using Variables
When constructing an object from variables, it can be quite repetitive if the key name matches the variable name.
If we are storing the value held by a variable in a key with the same name as that variable, we can omit the : and just write the name of the variable:
This example makes a user object with the provided properties as well as some default values (isAdmin is false and an empty friends list).
This is the same syntax used when exporting "named exports":
Destructuring
Destructuring is the process of creating variables from an existing array/object. When destructuring an object, the variable names must match the property key names that you wish to extract. The order in which you list the property names doesn't matter.
This is the exact same syntax used when importing a "named import".
When an object is passed to a function, we will often destructure the object directly in the parameter list, letting us choose only the properties of the object that are relevant to the function:
Last updated