Master the fundamentals of objects and arrays by applying them in a visualized tutorial
1: Arrays
6: Objects
11: Combining the Two
JavaScript objects and arrays get easier once you understand when to use them.
Objects and arrays are two common ways to store data in JavaScript. Unfortunately, it can be hard to choose when to use each one!
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.
You need to know the very basics of objects and arrays, which you can read about below.
Here is the task you need to complete
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.
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.
Let's take a look at how to organize our grills first
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!
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'
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.
Let's take a look at how to organize our grills first
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.
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.
You want to book the grill from 7PM-8PM. You live in the "eastvillage". Create your booking in the grillDay array!
Check if your booking starts at 7PM!
Incorporate transit times into your calculations
In our original parameters, we can see that we need to add an extra hour for transit and cleaning to every booking.
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'
The above statement means that the grill will be in transit from 9AM-10AM.
Incorporate transit times into your calculations
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.
Add transit time as an 8-9PM booking in the grillDay array. Use 'transit' to indicate transit time
Check if your booking starts at 8PM!
When to use objects v. arrays
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
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.
Let's take a look at how to organize our 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.
The object needs to have two key-value pairs. One for name, and one for location.
var order = { name: "dave", location: "chelsea" };
If your name was Dave and you lived in Chelsea, this is what your order would look like!
Create an order from a user
Data labels are a big deal with an order from a user. These are known as keys
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 = { };
Check if you got the keys right!
Change the values in your object
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';
If Katie from our previous example changed her location to 'wallstreet', this is what it would look like in code.
Switch the name associated with a user order
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'.
Check if you got the keys right!
Putting both grill-days and orders into the same data structure
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?
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.
Putting both grill-days and orders into the same data structure
We also need to figure out how to handle transit times. This can just be a new value for the 'location' property
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' };
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.
Putting both grill-days and orders into the same data structure
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.
Create your booking in the grillDay array!
grillDay[9] = { }; grillDay[10] = { };
Check if you got the keys right!
Remove orders from the array
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]={};
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.
Remove orders from the array
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!
Check if you got the keys right!
Interested in more? Email me at kevin (at) rtfmanual.io
var order = {};
var grillDay = [];