foreachain
The chainable forEach()
.
Why?
I don't really know. I just imagined that there can be a task like this:
- Output array data, element by element
- Modify array
- Output data again
And this can't be done in JavaScript in one long chained function call.
...well, OK, it can be done like this:
[2,3,5].map(v => {console.log(v);return v})
.map(v => v * 3)
.forEach(v => {console.log(v)});
But I find the first map
call unreadable and confusing. So, this is my solution.
How?
-
npm i foreachain
or
<script src="https://unpkg.com/foreachain"></script>
-
import foreachain from 'foreachain';
[].constructor.prototype.forEachain = foreachain;
-
[2,3,5].forEachain(v => {console.log(v)})
.map(v => v * 3)
.forEach(v => {console.log(v)});
Is this necessary to have as a separate package?
If you think that the answer should be "No", you can copy the source code over
to your codebase.
List of times I found use for a chainable forEach
I'm still looking...
License
MIT © 2020-2021 Nikita Karamov