How to use JavaScript Array flatMap()

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

  1. Declare two variables a and b
  2. 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)
  3. Array flatMap() method map all array elements and creates a new array.
  4. The value of b variable is displayed with the help of console.log inbuilt function