JavaScript expressions, objects and arrays
Content revised on 3 October 2026. Any completion marker earned before this revision refers to the earlier content version and does not confirm completion of this revised lesson.
Learning goals
- Transform object arrays using map, filter and reduce.
- Make immutable updates and test outputs without React.
Assumptions and scope
- Numeric minutes, boolean done and stable string IDs are supplied.
- The object copies here are shallow; nested data needs an explicit update strategy.
Content revised on 3 October 2026. Any completion marker earned before this revision refers to the earlier content version and does not confirm completion of this revised lesson.
React code is JavaScript code, so a small set of language skills removes much of the initial mystery. A value can be text, a number, a boolean, an object or an array. An expression produces a value that can be passed to a function or shown in JSX. A statement performs a step such as declaring a variable or returning from a function. Learn to distinguish them before trying to place a whole conditional statement inside JSX braces.
An object groups related fields. A study session can have an identifier, a duration and a completion flag. An array holds several such objects in an ordered collection. Access one field with a property name and inspect the shape before transforming it. Consistent records are easier to render and test than a mixture of unrelated types whose meaning depends on their array position.
The map method transforms each array item into a corresponding result. It is useful for producing labels or JSX elements. The filter method keeps items that satisfy a condition. It is useful for an active-only view. Neither method should be used as an excuse to mutate the original objects inside the callback. Returning a new array does not automatically protect objects shared with the original array.
Use object spread when changing one record: copy its existing fields, then overwrite the field that changes. Keep the other records unchanged. This produces a new collection with an explicit change instead of modifying data that another part of the interface may still be using. A const declaration prevents rebinding its variable name; it does not make an object or its nested values deeply immutable.
Functions make these operations reusable. Give the function explicit inputs and return a result rather than reaching into the page or hidden global data. A pure calculation can be tested without mounting React. For a total, reduce accumulates a result across the array; provide an initial value so an empty collection has a defined answer. Follow the calculation with a concrete prediction using a small data set.
Do not rush past language errors by installing another package. Read the value types and the callback return. A missing return inside a block-bodied map callback can create an array of undefined values. A string duration can also lead to unexpected addition. Our supplied records use numeric minutes and boolean completion flags deliberately. The practice asks you to transform them independently, then check both the result and the unchanged input. These skills support list rendering, derived counts and safe state updates throughout the rest of the course.
Official concept references (verified 3 October 2026): [MDN: arrays](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Scripting/Arrays) and [React: updating arrays](https://react.dev/learn/updating-arrays-in-state). Original data and solutions.
Worked example: Find unfinished study time
Use the supplied sessions array: reading 15 minutes is complete; practice 25 and review 10 are incomplete. Compute unfinished time without changing the records.
Solution
export const sessions = [
{ id: 'reading', minutes: 15, done: true },
{ id: 'practice', minutes: 25, done: false },
{ id: 'review', minutes: 10, done: false },
];
export function activeMinutes(items) {
return items.filter(item => !item.done).reduce((total, item) => total + item.minutes, 0);
}
Expected result: 35 minutes. Filtering keeps the two incomplete records; reducing adds 25 and 10 from an initial zero. The test freezes the input records, checks the result, and verifies further transformations do not mutate the source.
Read each row as a labeled relationship; no color or position is needed to understand it. All project data are fictional learning examples.
| Operation | Input | Output purpose |
|---|---|---|
| filter | Session records | Keep incomplete records |
| map | Each selected record | Produce labels, values or JSX |
| reduce | Selected numeric values | Accumulate a total |
Independent practice
-
For sessions, produce an array with every duration doubled.
-
For sessions, return the identifiers of incomplete entries.
-
Add title “Write a test” to the record whose id is “practice”, leaving all original records unchanged.
Practice answers and reasoning
-
sessions.map(item => item.minutes * 2) gives [30, 50, 20].
map returns one number per original record and does not change the minutes fields.
-
sessions.filter(item => !item.done).map(item => item.id) gives ["practice", "review"].
Filter first selects records; map then extracts the desired field.
-
sessions.map(item => item.id === "practice" ? { ...item, title: "Write a test" } : item)
The changed record is copied before its title is assigned. The original practice record still has no title field; unchanged records can retain their identities.
Common errors to avoid
- Forgetting return in a block-bodied callback.
- Copying an array while mutating a shared record inside it.
Exit check
-
Does const make an object deeply immutable?
-
Why give reduce an initial zero for this total?
Exit-check answers
-
No.
It prevents rebinding the variable, not changing its fields.
-
It defines the starting total and makes the empty-array result zero.
The calculation then has a meaningful result even when no sessions match.