5

Implementing a for loop in js because I need to skip some elements in a map as I transform. I'm feeling dirty

* cries in functional *

Comments
  • 0
    @24th-Dragon not exactly

    I am not skipping the transformation, but skipping the element from the array at all (as if it wasn't there)

    I am mapping a list of elements to a css grid in react

    some elements might take more than one cell span, so when I transform one element to a cell that might occupy the space of the next element, when I am iterating the next element I cannot know what happened to the previous element.

    One option *could be* use filter before, but I think a for loop is easier; I just move the index with the cell's span, and push to an array instead of returning; with that I was able to refactor my map to a for easily
  • 0
    @elvieeejo

    You do not need to write a for-loop.

    First do a "filter" on the list to get every data you want to transform and then do "transform" function.
  • 0
    @elvieeejo @mr-user this is actually what I was just about to write, filter your list then map.

    Might not be as performant, map, for each are about 80% slower than for loops* but much neater code.

    * read an article once, can't remember where though :(
  • 0
    I know, I already mentioned filter was a possible option in the comments, but for loop was easier to refactor to
Add Comment