FlatMap for nested collections
📖 Scenario: You work at a bookstore that organizes books by genre. Each genre has a list of book titles. You want to create a single list of all book titles from all genres.
🎯 Goal: Build a Swift program that uses flatMap to combine nested lists of book titles into one flat list.
📋 What You'll Learn
Create a dictionary called
booksByGenre with genres as keys and arrays of book titles as valuesCreate a variable called
allBooks that uses flatMap on booksByGenre.values to get a single array of all book titlesPrint the
allBooks array💡 Why This Matters
🌍 Real World
Combining nested lists of items is common when working with grouped data, like categories of products or genres of books.
💼 Career
Understanding how to flatten nested collections helps in data processing tasks, making it easier to analyze or display combined information.
Progress0 / 4 steps