Work with structured data using properties and methods
Objects store collections of key-value pairs. Think of a recipe card: it has a name, ingredients list, cooking time, difficulty level - each piece of information has a label (key) and value!
๐ก Real-World Example:
A dish object: { name: "Pizza", price: 12.99, rating: 4.5, ingredients: ["cheese", "tomato"] }
const dish = {
name: "Pasta Carbonara",
price: 15.99,
servings: 2,
isVegetarian: false
};
console.log(dish.name); // "Pasta Carbonara" (dot notation)
console.log(dish["price"]); // 15.99 (bracket notation)
dish.price = 17.99; // Update existing
dish.chef = "Mario"; // Add new property
delete dish.servings; // Remove property
const recipe = {
name: "Cake",
bake: function() {
return `Baking ${this.name}!`;
}
};
console.log(recipe.bake()); // "Baking Cake!"
const menu = { pasta: 12, pizza: 15, salad: 8 };
console.log(Object.keys(menu)); // ["pasta", "pizza", "salad"]
console.log(Object.values(menu)); // [12, 15, 8]
Create custom dishes by building object structures! Practice adding properties, nested objects, and methods.
Manage customer profiles with nested objects! Track orders, preferences, and loyalty points.
Create a dish object with calculated properties:
Example:
createDish("Pizza", 10, ["cheese", "pepperoni"])
โ { name: "Pizza", totalPrice: 14, description: "Pizza with 2 toppings" }
๐ก Hints:
Update a customer object and add new properties:
Example:
updateCustomer({ name: "Alice", orders: [], totalSpent: 50 },
75)
โ { ..., orders: [75], totalSpent: 125, loyaltyLevel: "silver" }
๐ก Hints:
{...customer}
(spread)
[...customer.orders, newOrder]
for orders