Master destructuring, spread operator, and data transformation
Modern JavaScript provides powerful tools to extract, copy, and transform data efficiently. These patterns help you work with complex recipe data, ingredient lists, and menu structures more elegantly!
๐ก Real-World Example:
Extract just the ingredients you need from a recipe object, or combine multiple menus into one!
const recipe = { name: "Cake", time: 45, servings: 8 };
const { name, time } = recipe; // Extract specific properties
console.log(name); // "Cake"
console.log(time); // 45
const ingredients = ["flour", "sugar", "eggs"];
const [first, second] = ingredients;
console.log(first); // "flour"
console.log(second); // "sugar"
const base = ["flour", "water"];
const full = [...base, "yeast", "salt"]; // Combine arrays
console.log(full); // ["flour", "water", "yeast", "salt"]
const menu1 = { pasta: 12, pizza: 15 };
const menu2 = { salad: 8, ...menu1 }; // Combine objects
console.log(menu2); // { salad: 8, pasta: 12, pizza: 15 }
const [first, ...rest] = ["egg", "milk", "flour", "sugar"];
console.log(first); // "egg"
console.log(rest); // ["milk", "flour", "sugar"]
Clean and extract structured data from messy recipe text! Practice string manipulation, parsing, and transformation.
Extract measurements from ingredient text and convert between units! Parse quantities, units, and ingredient names.
Extract essential recipe information:
Example:
cleanupRecipe({ name: "Soup", time: 30, ingredients: ["water",
"salt"] })
โ { name: "Soup", firstIngredient: "water" }
๐ก Hints:
const { name, ingredients } = recipe
const [first] = ingredients
{ name, firstIngredient: first }
Merge ingredient lists and add new items:
Example:
mergeIngredients(["flour", "sugar"], ["eggs"], "vanilla")
โ ["flour", "sugar", "eggs", "vanilla"]
๐ก Hints:
[...arr1, ...arr2, extra]