Lab 04: Arrays & Objects

🎯 Objective

Master JavaScript's two primary data structures — arrays (ordered collections) and objects (key-value maps) — including modern ES6+ destructuring, spreading, and array methods.

📚 Background

In JavaScript, arrays and objects are the building blocks of every data structure. Arrays are actually special objects with numeric keys. ES6 introduced powerful syntax: destructuring, spread, rest, shorthand properties, and computed property names. The functional array methods (map, filter, reduce, find, sort) eliminate most loops.

⏱️ Estimated Time

35 minutes

📋 Prerequisites

  • Lab 3: Functions, Scope & Closures

🛠️ Tools Used

  • Node.js 20

🔬 Lab Instructions

Step 1: Array Creation and Access

📸 Verified Output:

Step 2: Array Mutation Methods

📸 Verified Output:

Step 3: Functional Array Methods

📸 Verified Output:

Step 4: Sorting Arrays

📸 Verified Output:

Step 5: Object Basics and Methods

📸 Verified Output:

Step 6: Object Manipulation

📸 Verified Output:

Step 7: Map and Set

📸 Verified Output:

Step 8: Real-World Data Pipeline

📸 Verified Output:

✅ Verification

Expected output:

🚨 Common Mistakes

  1. Mutating during .map(): Always return a new value from map — don't mutate arr[i].

  2. sort() without comparator: Default sort is lexicographic — always pass (a,b) => a-b for numbers.

  3. const arr = []; arr = []: const prevents reassignment, not mutation — arr.push() is fine.

  4. Object spread is shallow: Nested objects are still shared references — use structuredClone() for deep copy.

  5. for...in on arrays: Iterates indices as strings and includes prototype properties — use for...of.

📝 Summary

  • Arrays: push/pop (end), shift/unshift (front), splice (anywhere), slice (copy)

  • Functional methods: map (transform), filter (select), reduce (accumulate), find (first match)

  • Sort: always provide (a, b) => a - b comparator for numbers

  • Object spread {...a, ...b} merges (right overwrites left); shallow copy only

  • Map for any-typed keys with O(1) lookup; Set for unique values

🔗 Further Reading

Last updated