Demystifying TypeScript Array Methods: map vs flatMap

Shivam Vishvakarma
2 min readJan 2, 2024

--

Introduction:

Arrays are fundamental data structures in JavaScript and TypeScript, offering a range of powerful methods for manipulation. Two commonly used array methods, `map` and `flatMap` (or `flat`), serve distinct purposes in transforming array elements. In this blog post, we’ll delve into the differences between these methods and guide you on when to use each.

1. map Method: Transforming Elements

#### Purpose:
The `map` method allows you to transform each element of an array based on a provided function, creating a new array with the transformed results.

#### Syntax:
const newArray = array.map((element, index, array) => {
// Transformation logic
return transformedElement;
});

#### Example:
const numbers = [1, 2, 3, 4];
const squaredNumbers = numbers.map(num => num * num);
// Result: squaredNumbers = [1, 4, 9, 16]

2. flatMap (or flat) Method: Mapping and Flattening

Purpose:
The `flatMap` (or `flat`) method not only transforms each element but also flattens the result into a single array. If the mapping function returns an array, `flatMap` automatically flattens it.

## Syntax:
const newArray = array.flatMap((element, index, array) => {
// Transformation logic that returns an array
return transformedArray;
});

##Example:
const words = [‘Hello’, ‘World’];
const characters = words.flatMap(word => word.split(‘’));
// Result: characters = [‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘W’, ‘o’, ‘r’, ‘l’, ‘d’]

Key Differences:

1. Flattening:
— `map`: Transforms elements without changing the structure of the array.
— `flatMap`: Transforms elements and flattens arrays returned by the mapping function.

2. Use Case:
— `map`: For transforming elements only.
— `flatMap`: For transforming elements and potentially flattening arrays returned by the mapping function.

3. Mapping Function:
— `map`: Returns a single value for each element.
— `flatMap`: Can return an array, which will be automatically flattened.

TypeScript Considerations:

Both `map` and `flatMap` work seamlessly in TypeScript, providing type checking for the transformed elements based on the mapping function.

//example
const numbers: number[] = [1, 2, 3, 4];
const squaredNumbers: number[] = numbers.map(num => num * num);

const words: string[] = [‘Hello’, ‘World’];
const characters: string[] = words.flatMap(word => word.split(‘’));

Conclusion: Choosing the Right Tool

Understanding the distinctions between `map` and `flatMap` empowers developers to choose the right tool for the task at hand. If you only need to transform elements, `map` is your go-to method. If you want to handle arrays returned by the mapping function and flatten the result, `flatMap` is the solution. By leveraging these array methods effectively, you can write cleaner and more expressive code in your TypeScript projects. Happy coding!

--

--

Shivam Vishvakarma

Hey, I'm Passionate about .Net, Angular, SQL, and Cloud tech. Join my coding journey! 🚀 Let's connect!