Javascript - Objects
Learning goals
What is an object
Accessing data using a key
Changing data using a key
Array of objects
Teacher instruction
Remember handin Sunday for data literacy!
Codelab
Divide your problem into smaller problems first
When a function returns something we prefix it with get.
getUpperCaseString,getUserDetailsRemember to read: Læs side 1-6 + kapitel 2 i storytelling bogen. For tomorrow
Reload gitbook!
Peer instruction 1 min
Question 1
What is the value of orResult?
truefalsetrue&&falseNone of the above
Question 2
What will the following code log out?
43
"43"
"age"
"User.age"
Syntax error
Flipped classroom videos
What is an object?
What problem does obejcts solve?
Let's say we wanted to store the name, age and email of a user with what we have learned so far. We could fx create 3 variables to hold this information:
This would work but we would very quickly run into problems when we get more users. What if we had 1000 users, now we would need to create 3000 variables 😱. That would obviously not work.
What about storing this data (name, age and email) in an array. Let's try that:
This is a bit better but what if we forgot where we stored the different values. Let's say we have 30 things we want to store on a user. Was the favorite food stored in index 27 or 28? Or what if we forgot where we stored these things? Now we would have to create an extra array that keeps that of where we store what information
Hmm now we are creating extra work and extra confusion. What if there was a data structure where we save information using a the name what we are storing (key).
Well we are in luck 👇
Objects
In an object we instead use a key to refer to some data. Objects are a key-value pair that can store any data type, including other objects. They're used to store data about a single thing and are often used to store data about a user or a product. Let's solve the problem we outlined above:
Here we create a new object with 3 keys: name, age and email. What is smart is that it is easy to access the kind of data we are interested in.
Accessing data using key
Let's try and access some of the data form the user object
We can access data at a key using either dot notation of square brackets. This is the same: console.log(user.email) and console.log(user['email']).
Changing data using key
We are not only able to access data using key, but we can also change it.
Array of objects
When working with data in javascript it is often in the form of an array of objects. Take a look at this json file that contain movie data. It is an array of objects. Let's take a closer look at it 🔎
Every object in the array represents a movie and has the following keys: title, year, rating, votes, running_times. If i wanted to get the second movies rating in javascript i would write
Exercises
📝 Exercise 1 - level 1
The objects below have some syntax issues - try and fix them all!
📝 Exercise 2 - level 1
Create an object that describes you. Fx a key called age with the value of 28
📝 Exercise 3 - level 1
Log the values of each property of kitten
📝 Exercise 4 - level 1
Write code in the space provided so that the expected values output
Exercises taken from here: https://syllabus.codeyourfuture.io/js-core-2/week-1/lesson
📝 Exercise 5 - level 2
The following object represents all the people that are in space right now.
Data is taken from here
Using the astronautsInSpace variable log out the following things
The number of astronauts in space right now
The name of the craft of the last person in the array
The lastname of the first astronaut in the ISS
📝 Handin
1 - What to wear
Create a function (that you have to name) that has temperature as parameter. Based on the temperature it should return a string with what the user should wear. You decide what the user should wear based on the temperature.
An example is:

2 - Dice game 🎲
Write a function that simulates a dice roll. You call the function with the number of times you would like to roll the dice.
Every time the dice hits a 6 log out You just hit 6!
Part 2
If the user hits 6 in every roll the log out Jackpot 🎉
3 - Build a sentiment analyzer
A sentiment analyzer is some functionality that figures out how positive/negative a sentence is.
Fx the sentence "I am mega super awesome happy" Should have a high score The sentence "I hate doing boring stuff" should have a low score.
Create a function that takes a string as a parameter. Calling the function will return an object with score, positiveWords and negativeWords. You decide how the score should be implemented and what words are negative and positive.
Here is an example of using the function:
4 - Credit card number formatter - optional
This is a very real world example of a problem i got at my previous work. I was tasked to implement one of the smart credit card input fields, where the credit card numbers are seperated with a space. Fx inputting 123456789 would show 1234 5678 9.

Create a function that takes a number as parameter. The function should return the following object:
Thins to consider:
What should happen if the function is called with an argument that is not a number?
Handin
Handin on Fronter here. The handin is the 26-09-2022 23:59
Handin the link for the code on GitHub!
When the handin date has passed you will be assigned a classmate to review
Review metrics:
Nævn en ting der inspirerede dig ved opgaven
Find et eksempel på en variabel der har et godt navn og et der har et dårligt navn. Begrund hvorfor
Vælg en af opgaverne og besvar om der var en tydelig og god struktur i koden? Begrund hvorfor/hvorfor ikke
Last updated