Handbooks / JavaScript / Chapter 5
Arrays and Objects in Practice
7 pages · ~5 min✓ Reviewed
Builds on Modern JavaScript Features. Next up: Event Loop and Async.
Array Methods Mental Model
1. map and filter
map(fn)→ new array, same length, one transformed value per elementfilter(fn)→ new array of elements where fn is truthy- filter length can shrink to 0, never grows
const nums = [1, 2, 3]; nums.map(x => x * 2); // [2, 4, 6] nums.filter(x => x > 5); // []
same length vs shrunk to 0
2. reduce
reduce((acc, x) => ..., init)folds an array into one valueacccarries the result between calls- Always pass init: empty array with no init throws TypeError
[].reduce((a, x) => a + x); // TypeError [].reduce((a, x) => a + x, 0); // 0 [1, 2, 3].reduce((a, x) => a + x, 0); // 6
init is also the result for an empty array
3. Mutation and chaining
| Method | Returns | Mutates original |
|---|---|---|
map | new array | no |
filter | new array | no |
reduce | one value | no |
forEach | undefined | no |
forEach returns undefined, so .forEach(...).map(...) crashes. Use map/filter to chain.
Searching, Testing and Sorting
1. Find and Test
| Method | Returns | On empty array |
|---|---|---|
find(fn) | first match or undefined | undefined |
findIndex(fn) | its index or -1 | -1 |
some(fn) | true if at least one passes | false |
every(fn) | true only if all pass | true |
Spread and Destructuring
1. Spread and Rest
- Array spread copies or merges:
[...a, ...b] - Object spread merges, later keys win
- Rest collects the remainder:
[head, ...tail],{ id, ...others } - Rest must come last
const all = [...a, ...b]; const cfg = { ...defaults, ...overrides }; // overrides.size replaces defaults.size
order decides the winner
2. Destructuring
| Array | Object | |
|---|---|---|
| Matches by | position | key |
Shallow vs Deep Copy and structuredClone
1. Shallow Copies
b = acopies the reference, not the data; mutating b also mutates a- Spread,
Object.assign,slice,Array.frommake shallow copies - Top level is new; nested objects are still shared
const o = { inner: { x: 0 } }; const c = { ...o }; c.inner.x = 1; console.log(o.inner.x); // 1
the shallow copy trap: o.inner is shared
2. Deep Copy
structuredClone(value)is a true deep copy- Handles Date, Map, Set, RegExp and circular references
- Drops class prototypes and getters
JSON.parse(JSON.stringify(x))is a lossy deep copy
Object.entries and Map/Set vs Plain Objects
1. Entries Round Trip
- Object.entries(obj) returns
[key, value]pairs of own enumerable string keys Object.keysgives keys only;Object.valuesgives values only
const obj = { a: 1, b: 2 }; Object.fromEntries( Object.entries(obj).map(([k, v]) => [k, v*2]) );
result { a: 2, b: 4 }: every value transformed
2. Map vs Plain Object
| Map | Object | |
|---|---|---|
| Keys | any type: objects, functions, NaN | coerced to string or symbol |
| Order |
Cheat Sheet
1. Transform and search
| Method | Returns |
|---|---|
map | new array, same length |
filter | subset, can be empty |
reduce(fn, init) | single value; no init on [] throws |
find | element or undefined |
findIndex | index or -1 |
some / every | any pass / all pass; every on [] is true |
2. Sorting
Check yourself
What do b === a and a print?
trueand[1, 10, 9]- No comparator means string order, and sort mutates
aand returns the same array
const a = [10, 9, 1]; const b = a.sort(); console.log(b === a, a);
What is copy after this JSON clone?
{ d: "2026-..." }: the Date is now a string- The
undefinedkeynis dropped;structuredClonewould keep both
const copy = JSON.parse(JSON.stringify(
{ d: new Date(), n: undefined }));What is x here?
null, not 5- Defaults apply only to
undefined
const { x = 5 } = { x: null };