JavaScript Data Objects

In Cadettes, you learn to write JavaScript Arrays – a list of items in code. The advanced form of data collection in coding is using JavaScript Data Objects. Multiple Arrays can be stored in a single Data Object. You still need a name, like arrays, and you still list things – but you can list multiple attributes. It looks like this:

A simple object code for survey answers, with three options:

var likePepperoni = {

No: 5,

Sometimes: 3,

Yes: 12

}

A more complicated object code for what to rate each soda flavor, where five people answered on a scale of 1-5:

var sodaRating = {

lemonLime: 4, 5, 1, 3, 4

plainCola: 3, 3, 5, 5, 1

orangeFizz: 2, 3, 5, 4, 4

rootBeer: 5, 4, 4, 2, 3

}

Objects should have one name, but they can be made of more than one word without spaces. The name of an object must not have spaces, cannot start with a number, or use special characters (*, %, &, etc.).  Most coders create names from two words that relate to the data the object contains: the first word is lowercase, and the second word is capitalized. This is typical, but not required.

You start with {, and end with }. These are called curly brackets – they show the computer the beginning and end of the object. 

The “var” at the beginning stands for variable. You have to add ‘var’ before for JavaScript to regognize your Data Object as a variable.

Here are two more simple examples about survey questions responses:

var funHobbies = {

handCrafts: 2,

videoGames: 8,

sportOrganization: 6

}

var serviceProjects = {

foodPantry: 5, 

homelessShelter: 6, 

animalShelters: 4

}

Seniors

Scouts make a Data Object of anything they can list. Mix up all the Data Objects and hand them out at random. See if they can guess which Data Object belongs to who.

If they need a direct guide for what to list – give them a printout of the 6 Shoes Vogue Recommends to list as a data object.

Ambassadors

Take it one step farther. Each line of the Data Object is called a property. In the Soda Rating example, there are five properties – five flavors of soda.

var sodaRating = {

lemonLime: 4, 5, 1, 3, 4

plainCola: 3, 3, 5, 5, 1

orangeFizz: 2, 3, 5, 4, 4

rootBeer: 5, 4, 4, 2, 3

}

If a Data Object is made about a person, each property would be a fact about that person.

{

   firstName: “Diana”,

   lastName: “Spencer”,

   title: “Princess of Wales”,

   children: “William”, “Harry”,

   countryName: “Great Britain”,

   yearBorn: 1961

   yearDied: 1997

          }

For Princess Diana, there are categories of information: first name, last name, her title, children’s names, the country’s name, year she was born, and year she died.

Next to each property, there is information specific to Princess Diana. This would change if you made a Data Object about a different person. The specific information is called the value. Value doesn’t mean it is valuable, but just that the information is specific for this data object.

Make a data object about their own self. Use first name, last name, favorite color, and a hobby they love. Then add two more categories they come up with (Place of birth, Proudest moment, Dream career, Number of siblings, Number of pets, Favorite sport, Favorite food, Favorite song). Now switch and try making one for a fellow scout.