Using Array.flatMap for Mapping and Flattening in Javascript


Apply a function to each element of the array and flatten the result.

Source Code

let arr = [1, 2, 3, 4];
let result = arr.flatMap(x => [x, x * 2]);
console.log(result); // [1, 2, 2, 4, 3, 6, 4, 8]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments