Special Topic 2: Complex Data Structures

Variables inside variables

We have learned a number of ways of storing information:

  1. Numbers, such as:
    var numberVariable = 42;
  2. Strings, such as:
    var stringVariable = "Hello world!";
  3. Arrays, such as:
    var marxArray = ["Harpo", "Groucho", "Chico"];
  4. And objects, such as:
    var chickenObject = {name: "Plucky", taste: "quite good!"};

However, most real data worth processing is complex, and combines many of these together.

These can be combined in many ways: arrays of objects, objects with array properties, arrays of arrays, and so on.

Let me give you a simple example using cars in a parking lot. Let's say that I have an object that I use to describe a car:


	var myCarObject = {
		make:	"Toyota",
		model:	"Yaris", 
		year:	2007,
		colour:	"blue"
		};
			

Great, there's one car. But there is more than one car in the lot. There are three! So let's add the others...


	var jeremysCarObject = {
		make:	"Ariel",
		model:	"Atom", 
		year:	2008,
		colour:	"deathly silver"
		};	

	var maysCarObject = {
		make:	"Rolls-Royce",
		model:	"Phantom", 
		year:	1956,
		colour:	"sterling silver"
		};	
			

So let's check that you understand this:

myCarObject.colour roll over for answer...
jeremysCarObject.year roll over for answer...
maysCarObject.colour roll over for answer...
jeremysCarObject.make roll over for answer...

An array of objects

Since we have three cars in the parking lot, it would make sense that we would link them together. In fact, we can join them into an array!

	var parkingLot = [
		myCarObject,
		jeremysCarObject, 
		maysCarObject
	];
			

Now we have an array with three cars inside it, each with their own properties. parkingLot now has a structure like this:

parkingLot [0] [1] [2]
make: "Toyota",   
model: "Yaris",
year: 2007,
colour: "blue"
make: "Ariel",
model: "Atom",
year: 2008,
colour: "deathly silver"   
make: "Rolls-Royce",
model: "Phantom",
year: 1956,
colour: "sterling silver"   

We now have an array of objects! Let's check to see if you understand this...

What is parkingLot[2].make? roll over for answer...
What is parkingLot[0].year? roll over for answer...
What is parkingLot[1].model? roll over for answer...
What is parkingLot[2].year? roll over for answer...

An object with an array

Here is another example. Let's say that I have a student named Pierson. Pierson is in three classes:


	var piersonsClassArray =[
		"Visual Arts 11",
		"Production Technology 11", 
		"Computer Programming 12"
	];
			

Let's say that I turn Pierson into an object in a rather dehumanizing way. One of his properties can be the array of classes...


	var piersonObject ={
		name: "Pierson",
		grade: 12, 
		classes: piersonsClassArray
	};
			

So let's check to see if you get this...

What is piersonObject.name? roll over for answer...
What is piersonObject.grade? roll over for answer...
What is piersonObject.classes[2]? roll over for answer...

An array of objects with an array

Let's say there are two more students...


	var zahraObject ={
		name: "Zahra",
		grade: 12, 
		classes: ["Calculus 12", "Computer Programming 12", "Visual Arts 12"]
	};

	var toriObject ={
		name: "Tori",
		grade: 12, 
		classes: ["Biology 12", "Computer Programming 12", "Theatre Arts 12"]
	};
			

And lets say that I join them into an array because they are all in the same class:


	var programmingStudentsArray =[
		piersonObject, 
		zahraObject,
		toriObject
	];
			

Now this is an interesting structure! Take a look at this:

programmingStudentsArray [0] [1] [2]
name: "Pierson",
grade: 11,
classes:
[0]Visual Arts 11 [1]Production Technology 11 [2]Computer Programming 12
name: "Zahra",   
grade: 12,
classes:
[0]Calculus 12 [1]Computer Programming 12 [2]Visual Arts 12
name: "Tori",   
grade: 12,
classes:
[0]Visual Arts 11 [1]Computer Programming 12 [2]Theatre Arts 12

So let's see if this makes sense!

What is programmingStudentsArray[2].grade? roll over for answer...
What is programmingStudentsArray[2].classes[2]? roll over for answer...
What is programmingStudentsArray[0].classes[1]? roll over for answer...

Complex data everywhere...

Complex data is everywhere. Here are some real-world examples:

Coming up? An array of arrays!