Array.from()

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — Building an Array From Scratch

Imagine you want to generate a brand-new array of 5 backpack slot numbers (`[0, 1, 2, 3, 4]`) or 50 level numbers without typing every single number by hand.

You can use `Array.from()` to build a new array from scratch. You give it two arguments: 1. An object specifying the desired size: `{ length: 5 }` 2. A callback function `(element, index) => ...` that returns the value to place at each index:

const slots = Array.from({ length: 5 }, (element, index) => index); console.log(slots);

Because we are creating a brand-new array from `{ length: 5 }`, the first parameter (`element`) is `undefined`, while the second parameter (`index`) counts from `0` to `4`.

Exercise 2 — Numbering From 1

Since `index` always starts at `0`, returning `index + 1` inside the function is the easiest way to generate a numbered list starting at `1` (like levels `1` to `5`):

const levels = Array.from({ length: 5 }, (element, index) => index + 1); console.log(levels);

You can return any calculation or text from that function. For example, here we generate the XP needed for each of the first 5 levels (`100`, `200`, `300`, `400`, `500`):

const xpTable = Array.from({ length: 5 }, (element, index) => (index + 1) * 100); console.log(xpTable);

Exercise 3 — Quiz: What Was Built?

What does this code print?

const a = Array.from({ length: 3 }, (e, i) => i * 2); console.log(a);
Options
• 0,2,4
• 2,4,6
• 0,1,2
• 3

Hint : The index starts at 0, then the function is applied to it.

Exercise 4 — Five Slots

Build an array of the numbers 1 to 5.

Hint : The length is how many you want, and the index needs a nudge to start at one.

Exercise 5 — Converting a String

`Array.from` also converts things that look like lists into real arrays. A string is one of them: it becomes an array of characters, which you can then treat like any array.

const letters = Array.from("Zap"); console.log(letters); console.log(letters.length);const upper = Array.from("Zap", (letter) => letter.toUpperCase()); console.log(upper.join("-"));

Exercise 6 — Quiz: From a String

What does this code print?

console.log(Array.from("abc").length);
Options
• 3
• 1
• undefined
• An error

Hint : Each character becomes its own element.

Exercise 7 — Replacing a Loop and push

Whenever you need to initialize a list of a fixed size—like 4 empty backpack slots, a countdown, or a row of grid cells—you would normally create an empty array `[]` and use a `for` loop with `.push()`:

const bagSlots = []; for (let i = 0; i < 4; i++) { bagSlots.push("Slot " + (i + 1) + ": empty"); } console.log(bagSlots);

`Array.from()` lets you build that exact same array directly in one step, without creating an empty array first or calling `.push()`:

const bagSlots = Array.from( { length: 4 }, (element, index) => "Slot " + (index + 1) + ": empty" ); console.log(bagSlots);

for loop or Array.from?

In everyday code, most developers still use a standard `for` loop because it is super easy to read. `Array.from()` is simply a handy one-line shortcut when you want to create the array inside a single `const` expression (especially in UI frameworks like React).

Exercise 8 — Array.from Cheat Sheet

Match each expression with its result.

Items to match
• Array.from({length: 3}, (e, i) => i)
• Array.from({length: 3}, (e, i) => i + 1)
• Array.from("ab")
• The second callback parameter
Definitions
• 0,1,2
• 1,2,3
• An array of 2 characters
• The index

Exercise 9 — Level Table

The game needs a level table showing how much XP each level costs. Build it without writing the numbers by hand.

Step 1: build `levels`, the numbers from 1 to 8. Step 2: build `xpTable`, a list of objects with a `level` and an `xp` property, where the XP of a level is the level times 50. Step 3: print one line per entry, in the format `"Lv.X: Y XP"`. Step 4: print `"Total XP: N"` with the sum of every XP cost, and `"Expensive levels: N"` with how many cost 300 XP or more.

Expected output:

Lv.1: 50 XP Lv.2: 100 XP Lv.3: 150 XP Lv.4: 200 XP Lv.5: 250 XP Lv.6: 300 XP Lv.7: 350 XP Lv.8: 400 XP Total XP: 1800 Expensive levels: 3
Starter code
// Write your code here
Back to the course