DEV Community

Cover image for Quick tip: using flatMap() to extract data from a huge set without having to write a loop
Christian Heilmann
Christian Heilmann

Posted on • Edited on • Originally published at christianheilmann.com

9 2

Quick tip: using flatMap() to extract data from a huge set without having to write a loop

I just created a massive dataset of all the AI generated metadata of the videos of the WeAreDeveloper World Congress" and I wanted to extract only the tags.

The dataset is a huge array with each item containing a description, generated title, an array of tags, the original and their title, like this:

{
  "description": "The talk begins with an introduction to Twilio…", 
  "generatedtitle: "Enhancing Developer Experience: Strategies ",
  "tags": ["Twilio", "DeveloperExperience", "CognitiveJourney"],
  "title": "Diving into Developer Experience"
}
Enter fullscreen mode Exit fullscreen mode

What I wanted was an alphabetical lost of all the tags in the whole dataset, and this is a one-liner if you use flatMap():

data.flatMap(d => d.tags);
Enter fullscreen mode Exit fullscreen mode

You can sort them alphabetically with sort():

data.flatMap(d => d.tags).sort();
Enter fullscreen mode Exit fullscreen mode

And you can de-dupe the data and only get unique tags when you use Set():

new Set(data.flatMap(d => d.tags).sort());
Enter fullscreen mode Exit fullscreen mode

You can try this in this codepen.

Image of Stellar post

How a Hackathon Win Led to My Startup Getting Funded

In this episode, you'll see:

  • The hackathon wins that sparked the journey.
  • The moment José and Joseph decided to go all-in.
  • Building a working prototype on Stellar.
  • Using the PassKeys feature of Soroban.
  • Getting funded via the Stellar Community Fund.

Watch the video

Top comments (2)

Collapse
 
ahmetcetin profile image
Ahmet Cetin

Who will tell Christian that flatMap is actually a loop? :)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay