[[TOC]]
Objects
This is how you create an object:
- Use camelCase for property names
const user = {
id: 12345,
firstName: "Solomon",
lastName: "Stroup",
age: 38
};
Dot Notation
You can use dot notation to "walk" through the object
user.id; // 12345
user.firstName; // "Solomon"
user.isAdmin; // undefined (property does not exist)
Redefining an object - updating its value
const user = {
id: 12345,
firstName: "Solomon",
lastName: "Stroup",
age: 38
};
user.lastName = "Funkhouser"; //Assuming I take my girlfriend's last name.
user.age = user.age + 1;
console.log(user); // {id: 12345, firstName: "Solomon", lastName: "Funkhouser", age: 39}
Things to remember:
- An object is a data type that allows you to group several variables together into one variable that contains keys and values.
- Use camelCase for property names (for example firstName instead of first_name).
- To read or update the value of a property, you can use the dot notation.
Define an object and return it in a function
function getProductDetails() {
const product = {
id: 3,
title: "The Product!",
inStock: true
}
return product
}
console.log(getProductDetails()); // the object you define
Getting and returning values from an object
/**
* @param {Object} city
* @param {string} city.name
* @param {number} city.value
*/
function getWeather(city) {
return `It's currently ${city.value} degrees in ${city.name}`
}
console.log(getWeather({name: "Amsterdam", value: 3})); // "It's currently 3 degrees in Amsterdam"
console.log(getWeather({name: "Brasilia", value: 24})); // "It's currently 24 degrees in Brasilia"
Incrementing values within an object
This one adds and returns the new object. Notice that const
doesn't make the object immutable
function incrementAge(person) {
person.age = person.age + 1;
return person;
}
// Sample usage - do not modify
const person = {
firstName: "Sam",
lastName: "Doe",
age: 18
}
Returning some html with variables
/**
* @param {Object} details
* @param {string} details.title
* @param {number} details.value
*/
export function renderTableRow(details) {
return `<tr>
<td>${details.title}</td>
<td>${details.value}g</td>
</tr>`
}
Working with an API and dot walking through the objects to get what you need.
The console.logs below don't work, they are used to traverse the objects returned from the API.
/** @param {Object} data */
export function getCountry(data) {
return(data.location.country)
console.log(data.location.country);
}
/** @param {Object} data */
export function getIcon(data) {
return(data.current.weather_icons[0])
console.log(data.current.weather_icons[0]);
}
/** @param {Object} data */
export function getTemperature(data) {
return data.current.temperature;
console.log(data.current.temperature);
}
Review
- An object is a data type that allows you to group several variables together into one variable that contains keys and values.
- In JavaScript, the recommended convention is camelCase for property names (for example firstName instead of first_name).
- To read or update the value of a property, you can use the dot notation.