Sub topic 13
Using the Counter Argument in Callbacks
This chapter assumes you already know map and filter on iterators, and that arrays need .values() before you can call helpers on them.
Array callbacks receive (element, index, array). Iterator helper callbacks receive two arguments: the value and a counter. The counter is a zero-based number that goes up by one for each value the helper pulls from the iterator directly before it in the chain. Helpers have no third argument, because an iterator has no backing collection to hand you.
The basic shape
const labels = ['a', 'b', 'c']
.values()
.map((value, index) => `${index}:${value}`)
.toArray();
console.log(labels); // ['0:a', '1:b', '2:c']These callbacks receive (value, counter): map, filter, flatMap, forEach, some, every, and find. The reduce callback receives (accumulator, value, counter).
Each helper counts its own input
The counter tells you the position within the helper's input stream. It does not tell you the position in the original source. Each stage in a pipeline keeps its own counter.
const result = ['a', 'b', 'c', 'd', 'e']
.values()
.filter((v, i) => i % 2 === 0) // i counts a,b,c,d,e
.map((v, i) => `${i}:${v}`) // i counts a,c,e
.toArray();
console.log(result); // ['0:a', '1:c', '2:e']filter counts every value it inspects, including the ones it rejects. map only sees the values filter lets through, so its counter runs 0, 1, 2. The same rule applies after drop:
const afterDrop = [10, 20, 30, 40]
.values()
.drop(2)
.map((v, i) => [i, v])
.toArray();
console.log(afterDrop); // [[0, 30], [1, 40]]If you need the original position, attach it at the start of the pipeline:
const withSourceIndex = ['a', 'b', 'c', 'd']
.values()
.map((value, index) => ({ value, index }))
.filter(({ index }) => index > 0)
.map(({ value, index }) => `${index}:${value}`)
.toArray();
console.log(withSourceIndex); // ['1:b', '2:c', '3:d']The counter in reduce
reduce handles the counter the same way Array.prototype.reduce handles its index. If you pass an initial value, the first callback call gets counter 0. If you leave it out, the first value becomes the accumulator, and the first callback call gets counter 1.
const seen = [];
[5, 6, 7].values().reduce((acc, v, i) => { seen.push(i); return acc + v; });
console.log(seen); // [1, 2]Common errors
| Mistake | What happens | Fix |
|---|---|---|
| Expecting the source index after `filter` or `drop` | The counter restarts for each stage | Attach the index with an early `map` |
| Using a third `array` argument | It is `undefined` | Keep your own reference if you need one |
| Passing a function that takes two parameters, such as `parseInt` | The counter is passed as the second argument | Wrap it in an arrow function |
Exercise
What does [3, 4, 5, 6].values().drop(1).filter((v, i) => i !== 1).map((v, i) => v * 10 + i).toArray() return?
Show answer
[40, 61]. After drop(1), filter sees 4, 5, 6 with counters 0, 1, 2 and rejects 5 (counter 1). map then sees 4 and 6 with counters 0 and 1, giving 40 and 61.
Exercise
In [2, 4, 8].values().reduce((acc, v, i) => acc + i, 100), what is the result, and how would it change without the initial value 100?
Show answer
With 100, the counters are 0, 1, 2, so the result is 103. Without it, the accumulator starts at 2 and the counters are 1 and 2, so the result is 5.