Flat Map in JavaScript
Syed Maaz Saeed

Syed Maaz Saeed @syedmaazsaeed

About: Full Stack Developer.! "404: Social life not found. Coding in progress. 🚧💻 "Debugging the glitches of life with a programmer's precision. 🔍💻"

Location:
Pakistan Punjab Bahawalpur
Joined:
Sep 13, 2023

Flat Map in JavaScript

Publish Date: Sep 30 '23
0 0

Flat Map in javascript is a great technique which you can learn here. Flat map essentially conbines techniques of map and filter Array method into one. I will suggest you to use flatMap() over combination of filter() and map().

FlatMap takes single pass and doesn’t produce intermediate array but filter ()and map() combination produces an intermediate array.

// using filterAndMap
console.time("filterAndMap")
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const squaredOddNumbers = numbers
.filter(num => num % 2 !== 0)
.map(num => num * num)

console.log(squaredOddNumbers); // [1, 9, 25, 49, 81]
console.timeEnd("filterAndMap")

Comments 0 total

    Add comment