Think of them like...a lookup table. Or dictionaries. Or card catalogs.
They are made up of keys and values.
An object can't have two keys with the same name. Each key has to be unique.
let person = {
name: 'Amari',
favoriteColor: 'green',
job: 'Software developer'
}
console.log(person.name)
// "Amari"
console.log(person.favoriteColor)
// "green"
console.log(person.job)
// "Software developer"
console.log(person.hometown)
// undefinedBoth work to retrieve data from objects. Bracket notation is useful when your key is in a variable or if the key is a string.
let lookupKey = 'job'
console.log(person[lookupKey])
// "Software developer"let person = {
name: 'Amari',
address: {
street: '4400 Nightingale Ct.',
city: 'Sassboro',
state: 'NC'
},
skills: ['Python', 'JavaScript', 'SQL', 'AWS'],
cool: true,
birthdate: new Date(1981, 2, 4)
}- Arrays are for lists of things, usually alike
- Objects are for grouping details about one thing
- You often combine them:
- arrays of objects
- objects with arrays as values
let team = [
{ name: 'Amari', yearsOfExperience: 4 },
{ name: 'Niki', yearsOfExperience: 1 },
{ name: 'Lennon', yearsOfExperience: 2 }
]
let totalYearsOfExperience = 0
for (let member of team) {
totalYearsOfExperience += member.yearsOfExperience
}let student = {
name: 'Ryan',
testScores: [91, 83, 88, 79, 95]
}
let course = {
title: 'Advanced Juggling',
students: [
{
name: 'Ryan',
testScores: [91, 83, 88, 79, 95]
},
{
name: 'Gray',
testScores: [89, 92, 90, 94, 87]
}
]
}Write a function called findPeopleByCity that takes two arguments, city and people. people will be an array of objects with the keys name and city. It should return an array of people who live in the specified city.
let people = [
{ name: 'Autumn', city: 'Durham' },
{ name: 'Parker', city: 'Raleigh' },
{ name: 'Kerry', city: 'Durham' }
]
findPeopleByCity('Durham', people)
// [
// {name: "Autumn", city: "Durham"},
// {name: "Kerry", city: "Durham"},
// ]
findPeopleByCity('Raleigh', people)
// [{name: "Parker", city: "Raleigh"}]