Learn JavaScript Objects/Arrays by Practicing

Master the fundamentals of objects and arrays by applying them in a visualized tutorial

Get Started

Nice Work!

Objects/Arrays Practice

Give Feedback

1: Arrays

6: Objects

11: Combining the Two

When to Use Objects and Arrays (0/15)

JavaScript objects and arrays get easier once you understand when to use them.

Why do I need this?

Objects and arrays are two common ways to store data in JavaScript. Unfortunately, it can be hard to choose when to use each one!

What is the focus?

We are going to use a highly visual method to help you understand each data structure. We are going to walk you through an example of building Grillber, the Uber for grills.

What do I need to know first?

You need to know the very basics of objects and arrays, which you can read about below.

Read this article first if you need to learn the basics

Go to Step 1

The Scenario (1/15)

Here is the task you need to complete

The Challenge

You have been tasked with building Grillber, the Uber for Grills. Here is how it works- a lot of people do not have the room to store a grill, or they grill too infrequently. This app will deliver a grill to people that order via their app. And then Grillber will pick the grill up when they are done.

Why is it in New York City?

New York has a lot of people that would like to grill, but live in tiny apartments. This is a good place to start because the popuation density is so high.

What are the key features of the app?

  • Users submit an order with name and location
  • Users book grills in one hour intervals
  • There needs to be an hour of travel time after each booking to bring the grill back to the warehouse and clean it.

Setting Up The Array (2/15)

Let's take a look at how to organize our grills first

A "Grill-Day"

Each grill can be booked for 24 hours a day (duh). So, we need a way to track whether each grill has already been booked or not. We need to use an array, since order matters a lot when planning a day!

Size of Array

The array needs to have 24 slots, one for each hour of the day. Check out the array to the right to see a grill that is available all 24 hours of the day.

grillDay[8] = 'wallstreet'

Creating A Booking

Let's just use the location to define the booking for now. The above statement means that the grill needs to be at Wall Street at 8AM.

Back
Show Me

Booking Specific Slots (3/15)

Let's take a look at how to organize our grills first

Why use an array?

Arrays make it really easy to create booking slots. Unike actual Uber, where length of the trip varies, these grills are booked in hour-long intervals. So index 0 of the array corresponds to midnight to 1am.

Position in the Array

So, if you wanted to check if the grill was booked from 4AM-5AM (I see you are nocturnal), all you would need to do is check index 4 of the array.

Your Turn

You want to book the grill from 7PM-8PM. You live in the "eastvillage". Create your booking in the grillDay array!

After you start typing, the array to the right will show live feedback!

Back
Check Answer

Check if your booking starts at 7PM!

Transit Times (4/15)

Incorporate transit times into your calculations

Two hours at once

In our original parameters, we can see that we need to add an extra hour for transit and cleaning to every booking.

Using index

Index determines the position within the array. Our first booking was index 8, the 9th element in the array. So this one needs to have an index of 9, or the 10th element in the array.

grillDay[9] = 'transit'

Add Transit time

The above statement means that the grill will be in transit from 9AM-10AM.

Back
Show Me

Transit Times (5/15)

Incorporate transit times into your calculations

Filling Out The Chapters

Many "chapters" of the array are still empty. That's alright. The data structure (array) makes it clear that you have 24 available slots, in this case. If you booked on half-hour intervals, then there would be 48.

Your Turn

Add transit time as an 8-9PM booking in the grillDay array. Use 'transit' to indicate transit time

After you start typing, the array to the right will show live feedback!

Back
Check Answer

Check if your booking starts at 8PM!

Intro to Objects (6/15)

When to use objects v. arrays

The Challenge

Now that we can book grills, we need to figure out how to format the orders from app Grillber users. This ensures the grill will be delivered successfully

What are the key features of the app?

  • Users submit an order with name and location
  • Users book grills in one hour intervals
  • There needs to be an hour of travel time after each booking to bring the grill back to the warehouse and clean it.

How should we store this information?

We are going to use an object here. Much like a newspaper, objects work well when you need to break your data up into sections. Order is not a big deal.

Setting up an Object (7/15)

Let's take a look at how to organize our order

Info with an Order

You can imagine that an incoming order from a user might have a bunch of useful info: name, location, phone number, cost etc. We will just stick to name and location right now.

Setting up fields

The object needs to have two key-value pairs. One for name, and one for location.

var order = { name: "dave", location: "chelsea" };

Creating An Order

If your name was Dave and you lived in Chelsea, this is what your order would look like!

Back
Show Me

Creating an Object (8/15)

Create an order from a user

Why use an object?

Data labels are a big deal with an order from a user. These are known as keys

Your Turn

Your name is "katie". Your location is the "westvillage". Create your booking in the order object! The two keys are name and location.

var order = { };

After you start typing, the object to the right will show live feedback!

Back
Check Answer

Check if you got the keys right!

Changing an Object (9/15)

Change the values in your object

Objects Use Labels

Since order is not the defining way that we organize objects, it is always easy to add a new key-value pair . Or, we can change an exisiting pair by assigning a new value

order[location] = 'wallstreet';

New Value in An Order

If Katie from our previous example changed her location to 'wallstreet', this is what it would look like in code.

Back

Changing an Object (10/15)

Switch the name associated with a user order

Your Turn

You want to switch the name on the order to your friend, Joe. Change the value of the name property in the order object to 'joe'.

After you start typing, the object to the right will show live feedback!

Back
Check Answer

Check if you got the keys right!

Combining the Two (11/15)

Putting both grill-days and orders into the same data structure

The Challenge

Okay. We now have a data structure for booking grills hour-by-hour, and another for orders. Now, how can we properly combine them into one sortable structure?

What are the key features of the app?

  • Users submit an order with name and location
  • Users book grills in one hour intervals
  • There needs to be an hour of travel time after each booking to bring the grill back to the warehouse and clean it.

How should we store this information?

We need to create an array full of objects. The array will still have 24 slots, but each order will be in the form of an object rather than a string.

Combining the Two (12/15)

Putting both grill-days and orders into the same data structure

Booking + Transit Times

We also need to figure out how to handle transit times. This can just be a new value for the 'location' property

Setting up fields

Each object still has two key-value pairs. One for name, and one for location.

grillDay[16] = { name: 'joe', location: 'westvillage' }; grillDay[17] = { name: 'joe', location: 'transit' };

Creating An Order

If your name was Joe, you ordered at 4PM, and you lived in the West Village, this is what your order would look like! We also include the second entry so we leave ample time to prep the grill for the next customer.

Back
Show Me

Combining the Two (13/15)

Putting both grill-days and orders into the same data structure

Why is this a good combination?

You need the labels provided by objects, while you need the order from arrays. You can book the grills for specific times without needing complex JavaScript for interpreting time within an object.

Your Turn

  1. Your name is "jordan".
  2. Your location is "gramercy".
  3. The two keys are name and location.
  4. You are booking the grill at 9AM, which is already shown.
  5. Make sure that the location of your second entry is 'transit'.

Create your booking in the grillDay array!

grillDay[9] = { }; grillDay[10] = { };

After you start typing, the object to the right will show live feedback!

Back
Check Answer

Check if you got the keys right!

Cancelling an Order (14/15)

Remove orders from the array

Remove an Order + Transit

We can easily remove an order by resetting the object to an empty object. All we need to know is the position in the array. We also need to remember to remove the transit period

grillDay[20] = {}; grillDay[21]={};

Resetting an Item in the Array

Let's say someone had created an order at 8PM, but then cancelled. We could use the code above to remove their order and the transit period from the grillDay array.

Back

Cancelling an Order (15/15)

Remove orders from the array

Your Turn

Joe from the West Village cancelled his order from 4PM. You need to erase his order from the array, as well as the transit time associated.

Erase those bookings in the grillDay array!

After you start typing, the object to the right will show live feedback!

Back
Check Answer

Check if you got the keys right!

Congrats, you finished!

Interested in more? Email me at kevin (at) rtfmanual.io

Array

Objects

var order = {};

Combining Both

var grillDay = [];

Advance to Next Step