Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript array.flatMap()
The array.flatMap() method of JavaScript is used to flatten the input array element into a new array.
The syntax is as follows −
array.flatMap(function callback(current_value, index, Array))
Let us now implement the array.flatMap() method in JavaScript −
Example
<!DOCTYPE html>
<html>
<body>
<h2>Ranking Points</h2>
<p>Is any point equal to 550 from the key-value pairs...</p>
<button onclick="display()">Result</button>
<p id="test"></p>
<script>
var pointsArr = [10, 20, 30, 40, 50, 60, 70, 80, 90, 1000];
var res = pointsArr.flatMap(val => [val + 10]).entries();
for (val of res) {
document.getElementById("test").innerHTML += val + "<br>";
}
function pointsFunc(points) {
return points == 550;
}
function display() {
document.getElementById("test").innerHTML = pointsArr.some(pointsFunc);
}
</script>
</body>
</html>
Output

Click the “Result” button above −

Example
<!DOCTYPE html>
<html>
<body>
<h2>Car Variant</h2>
<button onclick="display()">Result</button>
<p id="test"></p>
<script>
var carid = ["110", "230", "299", "399"];
var res = carid.flatMap(val => [val + 10]).entries();
for (val of res) {
document.getElementById("test").innerHTML += val + "<br>";
}
function display() {
document.getElementById("test").innerHTML = "Array after slicing some elements = "+carid.slice(1,4);
}
</script>
</body>
</html>
Output

Click the “Result” button −

Advertisements