BACK
Lessons6. The from creation operator: arrays
Lessons · 06

6. The from creation operator: arrays

When you already have a ready-made structure

If your values already sit in an array, a Promise, or any iterable (a Map or Set, say), you can turn them into an Observable with the from operator.

The key difference from of

of takes many arguments, and each one becomes a separate value. from takes a single structure and unpacks it into elements.

from(['a', 'b'])
// Sequence: next('a') → next('b') → complete()

Glossary

  • Iterable — anything you can walk with a for...of loop: an array, a string, a Map, a Set, a generator.

Your task

  1. The letters array is already there.
  2. Pass that array (the variable — not an empty array!) into from(...).
  3. The subscription already logs each letter with a prefix.
  4. Expected output: Letter: a → Letter: b → Letter: c.
Solution spoiler · click to reveal
const { from } = Rx;

const letters = ['a', 'b', 'c'];

const letters$ = from(letters);

letters$.subscribe(value => {
  console.log('Letter: ' + value);
});
script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...