Arrays & objects
Understand the idea
A product list suddenly appears in a different order after another screen sorts it. Both screens were holding the same .
Choose how to find a value
A task has a title and a completion state, so an object fits it. A day's tasks have an order, so an array fits the collection. You can combine the two instead of inventing separate like taskOneTitle and taskTwoTitle.
Reading names and positions
Object properties are read by name. Dot notation works for a known identifier; brackets let a variable supply the key. A missing property reads as . Reading another property from that missing value is what usually throws, so check the parent when data is optional.
Array positions start at zero: the first task is at index 0. length counts items, so the last position of a non-empty array is length minus one. Dot notation uses a written property name; bracket notation is useful when the name comes from another variable. Reading a property that is not there gives undefined.
| Tool | Use it for | Watch for |
|---|---|---|
| Object property | Find a named field such as task.title | A missing field reads as undefined |
| Array index | Find an item by position such as tasks[0] | The first index is 0, not 1 |
| push / pop | Add or remove at the end | Both change the existing array |
| Object or array spread | Create a shallow outer copy | Nested objects stay shared |
| Destructuring | Give selected fields or items local names | It does not deep-copy the values |
| Array.isArray(value) | Check specifically for an array | typeof an array is "object" |
A copy may still share data
Arrays use zero-based positions and have a length. The last valid index is one less than that length. Destructuring can give a few positions or fields useful local names, while a default handles an undefined value; it does not replace .
Updating without surprises
Some the collection. push, splice, and sort change an existing array. slice and spread make shallow copies. Shallow means nested objects are still shared, so copying the array alone does not make each record independent.
Follow the two results
When you need a new order without changing the original array, copy before sorting. For numbers, give sort a numeric comparator; its default compares string representations. When changing one record, copying that record too makes the intended boundary of the change visible.
The example uses map to produce an updated array. For each task, the object spread copies its fields into a new outer object, then done replaces that field. The original task stays unfinished. You will study map in more detail later; here, watch which object changes and which stays independent.
Mental model
FOLLOW THE VALUE
The initial array contains an object reference.
01 / 03Trace the transformation
Live example
map creates a new here, and spread creates a new record inside it. Both copies matter for keeping the original task unchanged.
Your console output appears here.
Guided challenge
Keep the original record unchanged
Return a new task object with done set to true, leaving tasks[0].done false.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
false true 1 Review
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.