DEV Community

Cover image for 🤫 Secret JavaScript Methods They Don't Want You To See (part 2)
Dominic Magnifico
Dominic Magnifico

Posted on

🤫 Secret JavaScript Methods They Don't Want You To See (part 2)

As a continuation to our secret JavaScript Methods series, we’re going to talk about a controversial topic. This is a method that globe heads definitely don’t want you to see. That’s right, you guessed it, I’m talking about flatMap()
Theory.

The flatMap Theory 🌎

The flatMap() method, introduced in ES2019, isn't as well-known as its counterparts, but its potential is vast. It combines the functionalities of map() and flat() to transform and flatten arrays simultaneously.

const numArray = [1, 2, 3, 4];

const multipliedArray = numArray.flatMap((num) => [num * 2]);

console.log(multipliedArray); // Output: [2, 4, 6, 8]
Enter fullscreen mode Exit fullscreen mode

The callback in flatMap() operates similar to map(), but it also flattens the resulting array by one level. If the callback returns an array, flatMap() assimilates its elements into the main array. The method performs this action recursively to flatten to a single depth.

MTV’s The Real World: FlatMap 🎭

Imagine handling data from various sources with varying structures and needing to merge and process them uniformly. Here's a scenario where flatMap() shines:

Let's say we have an array of user objects containing interests, but these interests are nested arrays due to data variability:

const users = [
  { name: 'Alice', interests: ['skiing', 'photography'] },
  { name: 'Bob', interests: ['gaming'] },
  { name: 'Charlie', interests: ['hiking', 'baking', 'skiing'] }
];
Enter fullscreen mode Exit fullscreen mode

To create a unified list of all interests across users, we'd traditionally use map() followed by flat():

const allInterests = users.map(user => user.interests).flat();
console.log(allInterests); // Output: ['skiing', 'photography', 'gaming', 'hiking', 'baking', 'skiing']
Enter fullscreen mode Exit fullscreen mode

However, with flatMap(), this becomes concise and elegant:

const allInterests = users.flatMap(user => user.interests);
console.log(allInterests); // Output: ['skiing', 'photography', 'gaming', 'hiking', 'baking', 'skiing']
Enter fullscreen mode Exit fullscreen mode

The flatMap() method elegantly handles the extraction and flattening in a single sweep, simplifying complex data transformations.

Embracing flatMap() 🤗

As the globe spins on its axis, so too does the world of JavaScript, mastering versatile methods like flatMap() enhances our coding arsenal and helps us to write clean, concise, maintainable code for the future. Whether it's flattening arrays, transforming data structures, or streamlining complex data handling, flatMap() stands as a super useful method to keep in your back pocket.

Top comments (4)

Collapse
 
joolsmcfly profile image
Julien Dephix

Hi.

Oh yeah, the good ole JavaScript ain't round! theory.

Now, all flatness considerations aside I would not say it has vast potential. It's barely sugar syntax (only saves 3 characters) and performance is almost the same as map followed with flat.

Official doc says

It is identical to a map() followed by a flat() of depth 1 (arr.map(...args).flat()), but slightly more efficient than calling those two methods separately.

Thanks for bringing it up though. People should RTFM more often to discover new methods.

Collapse
 
magnificode profile image
Dominic Magnifico

Vast may well be overselling the method a bit, but it's useful for sure!

I'm a big fan of scrolling through the docs and finding obscure methods I was not aware of 🤣.

Thanks so much for reading!

Collapse
 
voidsplit profile image
VoidSplit

Hi,

For part 3 you could talk about Labeled statement, it's not very well known, supported by all browsers and yet it's super useful!
Here's the official doc.

Collapse
 
magnificode profile image
Dominic Magnifico

Oh this is an excellent one to add to my list. Thanks so much for sharing!