This proposal introduces a new method to the Javascript Array prototype to support the common use case of performing side effects while iterating through an Array, while returning the original array paramenter. It is a pretty useful tool to help with the case where one finds themselves having to leave the chainability of function calls like .map
, .filter
and .reduce
RxJS is an awesome library that almost lets you treat asynchronous events as elements in an array (called an Observable). RxJS has a similarly named method on Observable
which is pretty useful.
The Array.prototype.do
method is essentially synctactic sugar on a call to Array.prototyp.map
that performs side effects before returning the first parameter of the callback.
For example:
myArray.do(console.log)
is equivalent to
myArray.map(x => {
console.log(x);
return x;
})
arr.do(function callback(currentValue, index, array) {
// Return element for new_array
}[, thisArg])
callback
Function that performs the intended side effect, taking three arguments:
currentValue
The current element being processed in the array.
index
The index of the current element being processed in the array.
array
The array do
was called upon.
thisArg
Optional. Value to use as this
when executing callback.
The original array that .do
was called on
Debugging (example inspired from RxJS docs
let clicks = ... // events
let positions = clicks
.do(console.log) // easy to add line when debugging
.map(ev => ev.clientX)
Filtering function applications
let allFiles = ... // list of files
allFiles
.do(someFunctionForAllFiles)
.filter(s => s.endsWith('.js'))
.do(someFunctionForJSFilesOnly)