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...ofloop: an array, a string, aMap, aSet, a generator.
Your task
- The
lettersarray is already there. - Pass that array (the variable — not an empty array!) into
from(...). - The subscription already logs each letter with a prefix.
- 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
CONSOLE · Console output
Hit Run to see the result...