Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
  • Register
  • Login
  1. Home
  2. Blogpost

flat() and flatMap() in JavaScript

Date- May 19,2023

3930

Hello guys and welcome to Code2Night. In JavaScript, the array methods flat() and flatMap() are powerful additions introduced in ECMAScript 2019 (ES10) that simplify working with nested arrays. These methods provide concise and efficient ways to handle arrays within arrays, making your code more readable and manageable. In this tutorial, we will delve into the functionalities of both flat() and flatMap() and understand how they can enhance your array manipulation skills. So, let's dive in and explore each method in detail.

flat() and flatMap()

In JavaScript, flat() and flatMap() are array methods introduced in ECMAScript 2019 (ES10) that allow you to work with nested arrays in a concise manner. Let's explore each method:

1.) flat(): The flat() method creates a new array by concatenating all subarrays within a nested array up to a specified depth.

Syntax:

 let newArray = array.flat(depth);
  • Parameters:

    • depth (optional): An integer specifying the depth level of flattening. If not provided, the default depth is 1.
    • Example:

 const nestedArray = [1, [2, 3], [4, [5, 6]]];
const flattenedArray = nestedArray.flat();

console.log(flattenedArray);
// Output: [1, 2, 3, 4, [5, 6]]
In the example above, flat() is called on the nestedArray with no depth specified, resulting in a new array where subarrays are concatenated up to a depth of 1. The nested subarray [5, 6] is not flattened because the depth is 1.

2.) flatMap(): The flatMap() method combines the functionality of map() and flat() in a single step. It maps each element of an array using a mapping function and then flattens the result into a new array.
Syntax:

 let newArray = array.flatMap(callback);
Parameters:

callback: A function that is called for each element in the array and returns the value to be included in the new array. It takes three arguments: currentValue, index, and array.
Example:

 const numbers = [1, 2, 3, 4, 5];
const doubledArray = numbers.flatMap(num => [num, num * 2]);

console.log(doubledArray);
// Output: [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]
In the example above, flatMap() is used to create a new array where each element of the original array is mapped to two elements: the original value and its double.

It's important to note that both flat() and flatMap() methods are non-mutating, meaning they do not modify the original array. Instead, they return a new array with the desired transformation applied.


S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Comments

Tags

Swagger UI
Swashbuckle
SwashbuckleAspNetCore
Rest API
Postman
Api Testing
ITextSharp
Export to Pdf
AspNet Core
AspNet
C#
View to Pdf in Aspnet
Scheduler
Fibonacci series in Java
Display Fibonacci Series
First C# Program
What is C?
C
C Programming
CodeLobster
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, India   info@code2night.com

Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • XML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • PDF Editor
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Built with for developers