0Pricing
JavaScript Academy · Lesson

Iteration Patterns

Iterate Map and Set with for...of; use entries/keys/values; convert to arrays; build tiny counters and unique lists.

Iteration Patterns is a free JavaScript Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Iteration overview

Goal: Learn simple, clear iteration.

  • Map: entries, keys, values
  • Set: values
  • Convert to arrays
  • Frequency counter & unique lists
Iteration Patterns — illustration 1

Map: entries/keys/values

Use for...of on a Map to get [key, value]; keys() and values() are also available.

// Map iterates as [key, value] pairs
const prices = new Map();
prices.set("apple", 3);
prices.set("banana", 2);

for (const [item, cost] of prices) {
  console.log(item, cost);
}

// You can also use keys() or values()
for (const k of prices.keys()) {
  console.log("key:", k);
}
for (const v of prices.values()) {
  console.log("value:", v);
}
Iteration Patterns — illustration 2

Set: simple loop

Set yields unique values in insertion order; duplicates are ignored.

// Set iterates values in insertion order
const colors = new Set(["red", "green", "red", "blue"]);

for (const c of colors) {
  console.log("color:", c);
}

console.log("size:", colors.size);
Iteration Patterns — illustration 3

Conversions with Array.from

Use Array.from to turn Maps/Sets into arrays (and pairs into a Map).

// Convert Map to array of pairs, and back
const map1 = new Map([["a", 1], ["b", 2]]);
const pairs = Array.from(map1); // [["a",1],["b",2]]
console.log("pairs:", pairs);

const map2 = new Map(pairs);
console.log("map2 has a?", map2.has("a"));

// Set to array of values
const set1 = new Set([1, 1, 2, 3]);
const arr = Array.from(set1); // [1,2,3]
console.log("arr:", arr);
Iteration Patterns — illustration 4

Frequency counter

A Map counter is a clear way to tally occurrences in a list.

// Count items in an array using Map
function countItems(list) {
  const counts = new Map();
  for (const x of list) {
    const n = counts.get(x) || 0;
    counts.set(x, n + 1);
  }
  return counts;
}

const fruits = ["apple", "banana", "apple", "apple", "banana"];
const counts = countItems(fruits);

// Print counts as "item: n"
for (const [name, n] of counts) {
  console.log(name + ":", n);
}
Iteration Patterns — illustration 5

Unique + membership

Use Set to build a unique list and for fast membership checks with has.

// Unique values with Set, quick membership test
const numbers = [2, 3, 2, 4, 3, 5];
const unique = Array.from(new Set(numbers));
console.log("unique:", unique);

// Quick membership with Set.has
const allowed = new Set([2, 5]);
console.log("allowed has 2?", allowed.has(2));
console.log("allowed has 4?", allowed.has(4));
Iteration Patterns — illustration 6

Map iteration quiz

Quick check: Best Map loop for key + value.

Iteration Patterns — illustration 7

Recap

Recap: Iterate Map with for...of to get [key, value], loop Set values directly, and convert with Array.from. Use a Map for counters and a Set for unique lists.

Iteration Patterns — illustration 8

Frequently asked questions

Is the “Iteration Patterns” lesson free?

Yes — the full text of “Iteration Patterns” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Iteration Patterns”?

Iterate Map and Set with for...of; use entries/keys/values; convert to arrays; build tiny counters and unique lists. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Iteration Patterns” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Map & Set essentials
  2. WeakMap & WeakSet — simple privacy and caches
  3. Iteration Patterns
← Back to JavaScript Academy