You are reading the article Examples To Implement Javascript Flatmap updated in October 2023 on the website Khongconthamnam.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested November 2023 Examples To Implement Javascript Flatmap
Introduction to JavaScript flatmapThe flatMap() function in JavaScript is defined as map the elements and making the resultant mapping elements into a new array. This means it makes an into a single dimension array. flatMap() function return type is an array([]).
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Example:
String[] flatMap=array.flatMap(); Where to Use?
When JavaScript functions return an Observable then the result will not be a stream. But it will return an instance of Observable. So, flatMap() function maps this instance into a stream.
An Observable is an object that produces a stream of events.
// [3, 2, 4, 3, 5, 4]
Difference between map(), flat(), and flatMap() Function
map() just maps the values, flat() function return the array as it is where as in flatMap() function returns single dimension array only.
How does flatMap() Work in JavaScript?flatMap() can be worked in JavaScript by applying flatmap() function on any array then return type becomes an array([]).
Syntax:
array.flatMap(function callback(presentValue, position, arrayReference)) { }
presentValue: Pass current array values.
position: Pass the index. It is an optional argument.
arrayReference: Pass array reference. It is also an optional argument.
Examples to Implement JavaScript flatmapFollowing are the examples to implement as given below:
Example #1 – flatmap() logic to division of elementsCode:
let arrayValues = [2, 4, 6, 8];Output:
Explanation:
Line 1 makes an array into a flat array.
Line 2 makes an array into a map array.
Line 3 makes an array into a flatMap array.
Example #2 – flatMap() logic to spilt() a stringCode:
let stringArray = ["Hi Hello How are you", "Where are you?", "Amardeep"];Output:
Explanation:
Line 1 splits the string by comma separator and stores in the map.
Line 2 splits the string by comma separator and stores in a flat map.
Example #3 – flatMap() concatenating numbers with other array var array = [4,5,6,7,8,9,10];Output:
Explanation:
Line 1 concatenates the numbers and return a flatmap.
Example #4 – flatMap() printing multiplesCode:
var array = [1,2,3,4,5,6,7,8,9,10];Output:
Explanation:
Line 1 gives 2 multiples logic up to 10 values.
Line 2 gives 4 multiples logic up to 10 values.
Line 3 gives 6 multiples logic up to 10 values.
Line 4 gives 8 multiples logic up to 10 values.
Line 5 gives 10 multiples logic up to 10 values.
Line 6 gives 12 multiples logic up to 10 values.
Line 7 gives 14 multiples logic up to 10 values.
Line 8 gives 16 multiples logic up to 10 values.
Example #5 – flatMap() with subtracting from an arrayCode:
var array = [100,200,300,400,500,600,700,800,900,1000];Output:
Explanation:
Line 1 gives logic to subtract 1 from an array.
Line 2 gives logic to subtract 2 from an array.
Line 3 gives logic to subtract 3 from an array.
Line 4 gives logic to subtract 4 from an array.
Line 5 gives logic to subtract 5 from an array.
Line 6 gives logic to subtract 6 from an array.
Line 7 gives logic to subtract 7 from an array.
Line 8 gives logic to subtract 8 from an array.
Example #6 – flatMap with even, odd, negative, and zeros from an arrayCode:
const array = [2,3,5,6,8,9,10,11,0,12,14,15,0,-1,-5,-6,-7,-9,0]; return array%2==0 ? [array] : [] }) return array%2==1 ? [array] : [] }) return array<0 ? [array] : [] }) return array==0 ? [array] : [] })Output:
Explanation:
Line 1 gives logic for displaying even numbers.
Line 2 gives logic for displaying odd numbers.
Line 3 gives logic for displaying negative numbers.
Line 4 gives logic for displaying a number of zeros from an array.
Example #7 – flatMap with key value pairCode:
var nameArray = ['Paramesh', 'Vivek','Amardeep','Venkatesh','Mahesh','Chinna' ]; var companyArray = ['Verinon', 'EDUCBA','Oracle','IBM','Wipro','TCS' ]; var coursesArray = ['Java', 'Python','C','C#','Spring','Servlets' ];Output:
Explanation:
Line 1 gives logic for displaying the names index and its corresponding value.
Line 2 gives logic for displaying the company index and its corresponding value.
Line 3 gives logic for displaying the course index and its corresponding value.
Recommended ArticlesThis is a guide to JavaScript flatmap. Here we discuss the Introduction and how does flatmap() works in javascript along with different examples and its code implementation. You may also look at the following articles to learn more –
You're reading Examples To Implement Javascript Flatmap
Update the detailed information about Examples To Implement Javascript Flatmap on the Khongconthamnam.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!