How to use JavaScript Array flatMap()

Array flatMap

On this page

Array flatMap() Method

Program to use JavaScript Array flatMap() method

let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    let b = a.flatMap((a) => a * 3);
    console.log(b);
    

Output

[
      3,  6,  9, 12, 15,
     18, 21, 24, 27, 30
    ]
    

Steps Description

  • Declare two variables a and b
  • Initialize two variables a and b with value of string a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and b = a.flatMap((a) => a * 3)
  • Array flatMap() method map all array elements and creates a new array.
  • The value of b variable is displayed with the help of console.log inbuilt function