A single variable is fine for one value. But the calculator has a list of foods, and each food has several pieces of information. For that, we need arrays and objects.
Arrays
An array is an ordered list:
const foods = ["Oatmeal", "Chicken and rice", "Greek yogurt"];
- Items are separated by commas.
- Indexes start at 0:
foods[0]is"Oatmeal". foods.lengthis3.foods.push("Apple")adds to the end.foods.includes("Rice")checks membership.foods.join(", ")turns it into a string.
console.log(foods[0]); // "Oatmeal"
console.log(foods.length); // 3
console.log(foods.at(-1)); // "Greek yogurt"
Objects
An object groups related values under named keys:
const meal = {
name: "Chicken and rice",
calories: 550,
protein: 35,
};
- Access with dot notation:
meal.name,meal.calories. - Or bracket notation:
meal["calories"]— useful when the key is in a variable. - Add a key:
meal.fat = 12;. - Objects can nest:
{ name: "...", macros: { protein: 35, carbs: 60 } }.
Arrays of objects
This is the shape of almost every list in every app:
const foods = [
{ name: "Oatmeal", calories: 320, protein: 12 },
{ name: "Chicken and rice", calories: 550, protein: 35 },
{ name: "Greek yogurt", calories: 150, protein: 15 },
];
Each item is an object. The array holds the list. To get the first food’s calories: foods[0].calories.
Three methods worth knowing now
const names = foods.map((food) => food.name);
// ["Oatmeal", "Chicken and rice", "Greek yogurt"]
const highProtein = foods.filter((food) => food.protein >= 20);
// [{ name: "Chicken and rice", ... }]
const chicken = foods.find((food) => food.name === "Chicken and rice");
// the matching object, or undefined
maptransforms every item and returns a new array.filterkeeps the items that pass a test, as a new array.findreturns the first matching item (orundefined).
None of them change the original array. That’s a good thing.
Destructuring and spread
Two shortcuts you’ll see everywhere:
const { name, calories } = foods[0];
// name = "Oatmeal", calories = 320
const copy = [...foods];
// a shallow copy of the array
Destructuring pulls properties into variables. Spread (...) copies items into a new array or object. We’ll use both more in React.
Try it
Run this, then add a fourth food and see how the outputs change.
Console
Console is quiet. Run the code to see output.
Edit the code, then hit Run (or Ctrl + Enter) and watch the console. This is a safe sandbox — you can't break the page.
Build the food list
the plan's verify stepCreate an array with at least three food objects. Each needs a name, calories, and protein. Log the names with map, filter the foods with at least 20g of protein, and use find to pull out one food by name.
Check the shape: foods[0].name should work. If it's undefined, the object keys are probably different.
The mistakes everyone makes
- Starting at index 1. Arrays start at 0.
- Typo in a key.
food.caloriereturnsundefinedif the key iscalories. - Forgetting that map/filter return new arrays. They don’t change the original.
- Using
findwhen you want all matches.findreturns one;filterreturns all. - Mutating while filtering/mapping. Keep the original intact; return a new value.
- Assuming an object is ordered. Arrays have order; object keys mostly don’t.
Arrays are ordered lists; indexes start at 0.
Objects group values under named keys; access with dot or brackets.
An array of objects is the standard shape for a list of records.
map transforms, filter selects, find returns the first match.
Destructuring pulls properties out; spread copies arrays and objects.
Next: loops and array methods — how to go through the whole food log and add up the totals.
Up next
Loops and array methods
Go through a list, add up the calories, filter the high-protein meals, and find one item. Learn the loop and the four array methods that cover most of it.