Rana Maninder Singh
Lesson 5 of 9JavaScript Basics

Arrays & Objects

Lesson opener

In one line: Arrays - ordered lists with the modern iterator methods (map, filter, find, reduce).

Why it matters: This shows up at work when you need to arrays - ordered lists with the modern iterator methods (map, filter, find, reduce).

Before this lesson: Functions & scope

Before you start

ResourceLink
PreviousFunctions & scope
NextDOM & events
Time~25 minutes

What you will learn

  • Arrays — ordered lists with the modern iterator methods (map, filter, find, reduce)
  • Objects — named properties that describe one entity
  • Destructuring and spread — the two shortcuts that make data handling read fluently

Section 1 — Arrays: ordered lists

An array holds an ordered list of values. Each item has an index — starting at 0 — and you access it with square brackets.

let cities = ["London", "Dubai", "Mumbai"]

console.log(cities[0])        // London
console.log(cities.length)    // 3
cities.push("Cairo")
console.log(cities)           // whole array

Expected result:

London
3
[ 'London', 'Dubai', 'Mumbai', 'Cairo' ]

Common methods:

MethodWhat it doesExample
push(item)Add to the endcities.push("Cairo")
includes(item)Is it in there?cities.includes("Dubai") → true
indexOf(item)First position, or -1cities.indexOf("Mumbai") → 2
join(", ")Combine into a stringcities.join(" → ")

Why it matters at work: Almost every job — the week's top sellers, the rows of a CSV, the users who have logged in today — starts as a list. Arrays are the container for lists.


Section 2 — Modern iteration: map, filter, find, reduce

Programmers almost never use a plain for loop over arrays anymore. The modern methods describe what you want instead of how to step through it.

map — transform every item, produce a new array of the same length:

let prices = [100, 250, 60]
let withTax = prices.map((p) => p * 1.18)
console.log(withTax)   // [ 118, 295, 70.8 ]

Expected result: [ 118, 295, 70.8 ]

filter — keep only items that match, produce a shorter array:

let scores = [88, 55, 91, 41, 70]
let passed = scores.filter((s) => s >= 60)
console.log(passed)   // [ 88, 91, 70 ]

Expected result: [ 88, 91, 70 ]

find — the FIRST item that matches, or undefined:

let scores = [88, 55, 91]
let firstHigh = scores.find((s) => s >= 80)
console.log(firstHigh)   // 88

Expected result: 88

reduce — combine all items into ONE value (a total, a product, a summary):

let amounts = [10, 20, 30]
let totalAmount = amounts.reduce((total, amount) => total + amount, 0)
console.log(totalAmount)   // 60

Expected result: 60

Plain English breakdown of reduce:

Why it matters at work: These four methods cover 90% of real array work. Translate natural language into code directly: "add tax to every price" → map; "keep the passing scores" → filter; "find the first senior" → find; "total the column" → reduce.


Section 3 — Objects: named properties

An object describes one thing by attaching properties to it. Read them with dot notation.

let customer = {
  name: "Priya",
  tier: "gold",
  points: 1240
}

console.log(customer.name)     // Priya
console.log(customer["name"])  // Priya — square-bracket form
customer.points = 1310         // update a property
console.log(customer.points)   // 1310

Expected result:

Priya
Priya
1310

Plain English breakdown:

Why it matters at work: An object is one record — one customer, one order, one product. APIs return JSON, which is JavaScript objects and arrays on the wire. Reading an API response means reading objects.


Section 4 — Objects in arrays

The classic real-world shape: an array of objects, one per record.

let orders = [
  { id: 101, customer: "Priya", total: 220 },
  { id: 102, customer: "Rana", total: 85 },
  { id: 103, customer: "Ana", total: 410 }
]

let goldOrders = orders.filter((o) => o.total >= 200)
console.log(goldOrders.map((o) => o.id))            // [ 101, 103 ]

let totalRevenue = orders.reduce((sum, o) => sum + o.total, 0)
console.log(totalRevenue)                            // 715

Expected result:

[ 101, 103 ]
715

Plain English: filter picks the orders with total >= 200; then map extracts just their ids into a small array. Separately, reduce adds every order's total into one revenue figure.

This is the shape of nearly all dashboard tables — you will construct and consume arrays of objects constantly.


Section 5 — Destructuring

Destructuring pulls values out of an array or object into named variables in one line.

// Object destructuring — grab properties by name
let { name, tier } = { name: "Priya", tier: "gold", points: 1240 }
console.log(name)   // Priya
console.log(tier)   // gold
// Array destructuring — grab items by position
let [first, second] = ["London", "Dubai", "Mumbai"]
console.log(first)    // London
console.log(second)   // Dubai

Why it matters at work: API responses and config objects contain many fields; destructuring lets you lift out exactly the piece you need with one readable line.


Section 6 — Spread ...

Spread copies all elements from an array (or properties from an object) into a new one.

let base = [1, 2, 3]
let extended = [...base, 4, 5]
console.log(extended)   // [ 1, 2, 3, 4, 5 ]
let defaults = { tax: 0.18, region: "EU" }
let order = { ...defaults, id: 99 }
console.log(order)   // { tax: 0.18, region: 'EU', id: 99 }

Expected result: { tax: 0.18, region: 'EU', id: 99 }

Important: Spread makes a copy, it does not modify the original. Use [...items].push(...) when you want a modified copy without touching the source array — this protects the original data and is the standard immutable pattern.

let original = [1, 2, 3]
let modified = [...original]
modified.push(99)
console.log(original)   // [ 1, 2, 3 ] — unchanged
console.log(modified)   // [ 1, 2, 3, 99 ]

Expected result:

[ 1, 2, 3 ]
[ 1, 2, 3, 99 ]

Essential commands

Command / actionWhat it does
arr.map(fn)Transform every item → new array
arr.filter(fn)Keep items matching fn → shorter array
arr.find(fn)First matching item, or undefined
arr.reduce(fn, initial)Combine all items into one value
arr.push(item)Add to the end
{ key: value }Object literal
obj.key / obj["key"]Read a property (dot / bracket)
obj.key = vUpdate a property
let { a, b } = objObject destructuring
let [x, y] = arrArray destructuring
[...items]Copy/spread an array or object

Practice

Do this now:

  1. Create practice5.js.
  2. Define let products = [{name:"Laptop", price:1200}, {name:"Mouse", price:25}, {name:"Desk", price:300}].
  3. Use map to build withTax — every product price × 1.18.
  4. Use filter to keep only products priced under 500.
  5. Use reduce to total all the original prices.
  6. Use find to get the product named "Mouse".
  7. Destructure { name, price } from the "Mouse" product and print name and price.
  8. Print each result to console.

Expected output:

[ 1416, 29.5, 354 ]
[ { name: 'Mouse', price: 25 }, { name: 'Desk', price: 300 } ]
1525
{ name: 'Mouse', price: 25 }
Mouse
25

Test yourself

  1. [10, 20, 30].map((n) => n + "-row") produces what?
Answer

["10-row", "20-row", "30-row"] — map transforms every item and returns a new array of the same length.

  1. Why is find different from filter?
Answer

find returns the first match (a single value, or undefined if none). filter returns a new array of ALL matching items, possibly empty. Use find when you expect one answer, filter when the result could be many.

  1. What does reduce return, and how does the total work?
Answer

reduce returns one combined value. The second argument (e.g. 0) is the starting bucket; each item gets folded into it, and the running total is passed forward. After the last item the accumulated value is returned.

  1. let shown = {...obj, newKey: 1} — what happens to obj?
Answer

Nothing. Spread creates a new object; obj is left unchanged. This is the immutable-copy pattern — you never mutate the source by spreading it.

  1. You have { id: 1, name: "A", price: 5 } and write let { name } = prod. What is name?
Answer

"A". Object destructuring pulls the value of the property named name into a local variable called name. Only the listed keys are extracted.


Summary

Arrays hold ordered lists; objects hold named properties; together they are the shape of every API payload. Use map to transform, filter to narrow, find to locate, and reduce to total. Destructuring unpacks data into readable variables, and spread copies without mutating — the foundation of the style you will see in real JavaScript codebases.

Verified against ECMAScript 2023 / Node.js LTS documentation, September 2026.