Array.flatMap() Method
Array.flatMap() combines mapping and flattening of nested arrays within an array, simplifying data transformation and manipulation by applying a function and then flattening the result.
Syntax:
let A = array.flatMap(function callback(current_value, index, Array)) {
// It returns the new array's elements.
}
Example: In this example, the flatMap() method applies a function to each element of the num1 array, doubling and squaring each number, and then flattens the resulting arrays into a single-dimensional array.
Javascript
const num1 = [1, 2, 3, 4]; const result = num1.flatMap(number => [number * 2, number ** 2]); console.log(result); |
[ 2, 1, 4, 4, 6, 9, 8, 16 ]
JS 2019 – ECMAScript 2019
ECMAScript 2019, also known as ES10, introduced features like Array.flat(), Array.flatMap(), Object.fromEntries(), and Symbol. description, and some string methods, for enhancing JavaScript’s capabilities and expressiveness.
JavaScript 2019 (ES10) or ECMAScript 2019 new features are:
Name |
Description |
String.trimStart() |
trimming leading whitespace characters from a string. |
String.trimEnd() |
removes trailing whitespace characters from the end of a string |
Object.fromEntries |
Creates an object from a key-value pairs array. |
Optional catch binding |
omitting the catch parameter in try-catch blocks, simplifying error handling |
Array.flat() |
flattening nested arrays to a single-dimensional array. |
Array.flatMap() |
mapping and flattening nested arrays simultaneously for streamlined operations. |
Revised Array.Sort() |
Equal value elements retain relative positions during sorting. |
Revised JSON.stringify() |
safely handles UTF-8 code points (U+D800 to U+DFFF) accurate compatibility with JSON.parse(). |
Separator symbols allowed in string litterals |
separator symbols are allowed within numeric and string literals to improve readability without affecting their values |
Revised Function.toString() |
converts a function to a string representation |
We will explore all the above methods along with their basic implementation with the help of examples.