> For the complete documentation index, see [llms.txt](https://chasethestar.gitbook.io/ko.learn-rxjs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chasethestar.gitbook.io/ko.learn-rxjs/rxjs/operators/transformation/scan.md).

# scan

#### signature: `scan(accumulator: function, seed: any): Observable`

## Reduce over time.

***

💡 You can create [Redux](http://redux.js.org)-like state management with scan!

***

[![Ultimate RxJS](https://drive.google.com/uc?export=view\&id=1qq2-q-eVe-F_-d0eSvTyqaGRjpfLDdJz)](https://ultimatecourses.com/courses/rxjs?ref=4)

### Examples

**Example 1: Sum over time**

( [StackBlitz](https://stackblitz.com/edit/typescript-ltcl9d?file=index.ts\&devtoolsheight=100) )

```js
// RxJS v6+
import { of } from 'rxjs';
import { scan } from 'rxjs/operators';

const source = of(1, 2, 3);
// basic scan example, sum over time starting with zero
const example = source.pipe(scan((acc, curr) => acc + curr, 0));
// log accumulated values
// output: 1,3,6
const subscribe = example.subscribe(val => console.log(val));
```

**Example 2: Accumulating an object**

( [StackBlitz](https://stackblitz.com/edit/typescript-vu63kz?file=index.ts\&devtoolsheight=100) | [jsBin](http://jsbin.com/fusunoguqu/1/edit?js,console) | [jsFiddle](https://jsfiddle.net/btroncone/36rbu38b/) )

```js
// RxJS v6+
import { Subject } from 'rxjs';
import { scan } from 'rxjs/operators';

const subject = new Subject();
//scan example building an object over time
const example = subject.pipe(
  scan((acc, curr) => Object.assign({}, acc, curr), {})
);
//log accumulated values
const subscribe = example.subscribe(val =>
  console.log('Accumulated object:', val)
);
//next values into subject, adding properties to object
// {name: 'Joe'}
subject.next({ name: 'Joe' });
// {name: 'Joe', age: 30}
subject.next({ age: 30 });
// {name: 'Joe', age: 30, favoriteLanguage: 'JavaScript'}
subject.next({ favoriteLanguage: 'JavaScript' });
```

**Example 3: Emitting random values from the accumulated array.**

( [StackBlitz](https://stackblitz.com/edit/typescript-lb8aw9?file=index.ts\&devtoolsheight=100) )

```js
// RxJS v6+
import { interval } from 'rxjs';
import { scan, map, distinctUntilChanged } from 'rxjs/operators';

// Accumulate values in an array, emit random values from this array.
const scanObs = interval(1000)
  .pipe(
    scan((a, c) => [...a, c], []),
    map(r => r[Math.floor(Math.random() * r.length)]),
    distinctUntilChanged()
  )
  .subscribe(console.log);
```

**Example 4: Accumulating http responses over time**

( [StackBlitz](https://stackblitz.com/edit/rxjs-scan-accumulate-request-responses?file=index.ts\&devtoolsheight=100) )

```js
// RxJS v6+
import { interval, of } from 'rxjs';
import { scan, delay, repeat, mergeMap } from 'rxjs/operators';

const fakeRequest = of('response').pipe(delay(2000));

// output:
// ['response'],
// ['response','response'],
// ['response','response','response'],
// etc...

interval(1000)
  .pipe(
    mergeMap(_ => fakeRequest),
    scan < string > ((all, current) => [...all, current], [])
  )
  .subscribe(console.log);
```

### Related Recipes

* [Alphabet Invasion Game](/ko.learn-rxjs/rxjs/recipes/alphabet-invasion-game.md)
* [Battleship Game](/ko.learn-rxjs/rxjs/recipes/battleship-game.md)
* [Breakout Game](/ko.learn-rxjs/rxjs/recipes/breakout-game.md)
* [Car Racing Game](/ko.learn-rxjs/rxjs/recipes/car-racing-game.md)
* [Catch The Dot Game](/ko.learn-rxjs/rxjs/recipes/catch-the-dot-game.md)
* [Click Ninja Game](/ko.learn-rxjs/rxjs/recipes/click-ninja-game.md)
* [Flappy Bird Game](/ko.learn-rxjs/rxjs/recipes/flappy-bird-game.md)
* [Matrix Digital Rain](/ko.learn-rxjs/rxjs/recipes/matrix-digital-rain.md)
* [Memory Game](/ko.learn-rxjs/rxjs/recipes/memory-game.md)
* [Platform Jumper Game](/ko.learn-rxjs/rxjs/recipes/platform-jumper-game.md)
* [Progress Bar](/ko.learn-rxjs/rxjs/recipes/progressbar.md)
* [Smart Counter](/ko.learn-rxjs/rxjs/recipes/smartcounter.md)
* [Space Invaders Game](/ko.learn-rxjs/rxjs/recipes/space-invaders-game.md)
* [Stop Watch](/ko.learn-rxjs/rxjs/recipes/stop-watch.md)
* [Tank Battle Game](/ko.learn-rxjs/rxjs/recipes/tank-battle-game.md)
* [Tetris Game](/ko.learn-rxjs/rxjs/recipes/tetris-game.md)
* [Uncover Image Game](/ko.learn-rxjs/rxjs/recipes/uncover-image-game.md)

### Additional Resources

* [scan](https://rxjs.dev/api/operators/scan) 📰 - Official docs
* [Aggregating streams with reduce and scan using RxJS](https://egghead.io/lessons/rxjs-aggregating-streams-with-reduce-and-scan-using-rxjs) 🎥 - Ben Lesh
* [Updating data with scan](https://egghead.io/lessons/rxjs-updating-data-with-scan?course=step-by-step-async-javascript-with-rxjs) 🎥 💵 - John Linquist
* [Transformation operator: scan](https://egghead.io/lessons/rxjs-transformation-operator-scan?course=rxjs-beyond-the-basics-operators-in-depth) 🎥 💵 - André Staltz
* [Build your own scan operator](https://blog.strongbrew.io/build-the-operators-from-rxjs-from-scratch/?lectureId=scan#app) 🎥 - Kwinten Pisman

***

> 📁 Source Code: <https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/scan.ts>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chasethestar.gitbook.io/ko.learn-rxjs/rxjs/operators/transformation/scan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
