Explore Objects

Objects

Let's explore objects!

Explore:

  1. Access the value 'USA' from the courseInstructor object.
  2. Add key-value pairs to the courseInstructor object.
  3. Write your own object and access and manipulate the values.
// Our courseInstructor object
let courseInstructor = {
name: 'Jeremy',
age: 32,
livesAround: 'SF Bay Area',
country: 'USA'
}
// Let's access the object and object items
// Here is the object
console.log(courseInstructor);
// Here are two different ways to print a value from given the related key
console.log(courseInstructor['name']);
console.log(courseInstructor.name);
// Here is how to change a value
console.log(courseInstructor);
courseInstructor.age = 33;
console.log(courseInstructor);
// Here is how you add a key-value pair
console.log(courseInstructor);
courseInstructor.favColor = 'teal';
console.log(courseInstructor);
// You can get just the keys or values and put them in array
let courseInstructorArrayWithValues = Object.values(courseInstructor);
console.log(courseInstructorArrayWithValues);
// Note that the keys are all strings.
let courseInstructorArrayWithKeys = Object.keys(courseInstructor);
console.log(courseInstructorArrayWithKeys);