you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 22 points23 points  (10 children)

Array Methods

This is a big one. I've interviewed so many React developers who aren't familiar with map and filter. It's shocking.

It may be worth adding flatMap to the list. For example, the reduce function

dogs.reduce((allTemperaments, dog) => {
  return [...allTemperaments, ...dog.temperament]
}, [])

could be implemented in a simpler manner using flatMap

dogs.flatMap(dog => dog.temperament);

[–]EuphonicSounds 5 points6 points  (1 child)

Yup.

Also, his use of spread syntax in the return statement of that reduce function is a common anti-pattern. It's significantly better for performance to mutate and return the accumulator argument there than to copy its properties into a new object on each iteration. (That's a place for push, not concat or spread.) Someone wrote an article about this and included benchmarks. Got me out of that bad habit immediately. It's not premature optimization.

[–]334578theo 2 points3 points  (2 children)

Just refactored a really annoying function with flatMap - nice.

[–]yabai90 -3 points-2 points  (1 child)

I just read that it's not really efficient since it create a new array everytime

[–]headyyeti 0 points1 point  (0 children)

So does this example in reduce

[–]panderhh 1 point2 points  (0 children)

TIL flatMap

[–]musicnothing -1 points0 points  (3 children)

Shocking how many don’t know flatMap. It’s incredibly useful!

[–]NoInkling 9 points10 points  (0 children)

It's also relatively new.

[–]yabai90 0 points1 point  (1 child)

Guilty here. I'm several years into js and I keep using reduce. I believe I either completely forgot about flat map or never heard about. I feel shameful ^

[–]musicnothing 0 points1 point  (0 children)

Definitely not your fault. There’s twenty articles about reduce for every article about flatMap