Tracking Data Using Objects Flashcards
What is one way to think of a JavaScript object literal?
As a package of variables.
A JavaScript object literal is like a series of named variables, each with their own value, packaged into a single item - the object.
What code examples would change the weight property?
var animal = { name : “Zebra”, lifeSpan: 25, weight: 770 };
animal.weight = 770;
animal[“weight”] = 770;
Use a for in loop to log each of the property names of the shanghai object to the console.
var shanghai = {
population: 14.35e6,
longitude: ‘31.2000 N’,
latitude: ‘121.5000 E’,
country: ‘CHN’
};
Now that you are logging out the property names, include the property values too. In other words you want to log out 4 lines that include both the property name and value. For example: “population: 1435e6”
for(var key in shanghai) {
console.log(key, “: “, shanghai[key]);
}
Inside the array literal, add three object literals. In other words, the objects array should have three values. Each object should have 2 property/value pairs.
var objects = [];
var objects = [ { key: "value", property: "value" }, { key: "value", property: "value" }, { key: "value", property: "value" } ];
Given the following JavaScript code:
var car = {
make: “Ford”,
model: “Fiesta”,
year: 2015
}
for ( property in car ) {
console.log( property );
}
What will you see in the JavaScript console?
Given the following JavaScript object:
var student = {
userName: “Dave”,
email: “dave@teamtreehouse.com”,
badges: 130,
points: 14888
}
Which code example will display the values “Dave”, “dave@teamtreehouse.com”, 130 and 14888 to the JavaScript console?
for ( var prop in student ) {
console.log( student[prop] );
}
What does JSON stand for?
JavaScript Object Notation
JSON is:
a string.
JSON is a string that’s formatted LIKE a JavaScript object literal.
Complete the code below to display every property and value of an object named location:
_____ ( property _____ location ) {
document.write(‘
’ + property + ‘:’ + location[property] + ‘
’);
}
The indexOf( ) method can search an array to see if there is a matching item in the array. For example, students.indexOf(“Kari”) looks to see if the string “Kari” is an item in the students array. What value does the JavaScript interpreter return if it CAN’T find a matching item in the array?
-1
Complete the following code to open an alert with the first item in the scores array:
alert( scores[_____] );
0
Complete the code below. Use a method you were taught in this course to remove the last item from the students array.
var lastStudent = students._____;
pop()
T/F: A two-dimensional array is an array of objects.
False
A two-dimensional array is an array, where each item in the array is another array.
Complete the code below to display every property and value of an object named location:
( _____ property _____ location ) {
document.write(‘
’ + property + ‘:’ + location[property] + ‘
’);
}
for, in