6
irene
36d

The forEach in JavaScript makes no sense. It looks like a map /filter/reduce but doesn’t actually return the array. Can you please loop like a normal language?

Comments
  • 6
    If you want to process the array, just use map/filter, foreach is for using the items in a loop, it's not useless.
  • 4
    Naming suggests it does not return anything.
  • 4
    It's basically why "map" and "filter" were created...
  • 0
    Foreach is the side effect in the function chain. It's not my preferred method, but it tracks.
  • 2
    Now I'm more interested in a language that returns a value from said list that's called foreach() 🤨

    It's really no different to expecting while(true) or for(i in 100000) to never return anything.
  • 1
    @theabbie @SortOfTested @C0D4 I get that it exists and that it works. I even use it. I just kind of hate it.

    for(let x of foo) {
    // 🖖
    console.log(x);
    }

    const foo = foo.map((x)=> typeof x); // 🤟

    foo.forEach((x)=> console.log(x)); // 🦶
  • 1
    @irene
    Sure, it's easy to hate

    But
    foo.forEach(console.log);
  • 0
    @SortOfTested Hah. I didn't think about that.

    I guess the part that I hate most is when people have a giant functional chain then plop a forEach at the end. foo.filter(...).map(...).reduce(...).filter(...).map(...).forEach(...)
Add Comment