JavaScript contains many arrays (or 2-d array) and the task is to flatten the array and make that look like 1-d JavaScript array. There are two approaches that are discussed below. You can also use Underscore.js _.flatten() with Examples.
Approach 1: Use Array.prototype.concat.apply() method to perform the operation. The concat() and apply() method is used to concat the arrays to 1-d array.
- Example:
<!DOCTYPE HTML><html><head><title>How to flatten array withthe JavaScript?</title><style>body {text-align: center;}h1 {color: green;}</style></head><body><h1>GeeksforGeeks</h1><pid="GFG_UP"></p><buttononClick="GFG_Fun()">click here</button><pid="GFG_DOWN"></p><script>var up = document.getElementById('GFG_UP');var down = document.getElementById('GFG_DOWN');var arr1 = [1, 2, 3];var arr2 = [4, 5, 6, 7];var arr = [arr1, arr2, 8, 9];up.innerHTML = "Click on button to get "+ "the common elements from these"+ " array <br>Array -[[" + arr[0]+ "], [" + arr[1] + "], " + arr[2]+ ", " + arr[3] + "]";function GFG_Fun() {down.innerHTML =Array.prototype.concat.apply([], arr);}</script></body></html>chevron_rightfilter_none -
Output:

Approach 2: The $.map() method in jQuery can be used to perform the operation. This method takes the array and a method as input. The second argument which is a method takes elements of original array one by one and return its elements.
-
Example:
<!DOCTYPE HTML><html><head><title>How to flatten array withthe JavaScript?</title><style>body {text-align: center;}h1 {color: green;}</style><scriptsrc=</script></head><body><h1>GeeksforGeeks</h1><pid="GFG_UP"></p><buttononClick="GFG_Fun()">click here</button><pid="GFG_DOWN"></p><script>var up = document.getElementById('GFG_UP');var down = document.getElementById('GFG_DOWN');var arr1 = [1, 2, 3];var arr2 = [4, 5, 6, 7];var arr = [arr1, arr2, 8, 9];up.innerHTML = "Click on button to get the"+ " common elements from these array"+ "<br>Array - [[" + arr[0] + "], ["+ arr[1] + "], " + arr[2] + ", "+ arr[3] + "]";function GFG_Fun() {down.innerHTML = $.map(arr, function(n) {return n;});}</script></body></html>chevron_rightfilter_none -
Output:

Recommended Posts:
- Underscore.js | _.flatten() with Examples
- Lodash _.flatten() Method
- Difference between array.size() and array.length in JavaScript
- What is the difference between Array.slice() and Array.splice() in JavaScript ?
- How to convert Integer array to String array using JavaScript ?
- How to move an array element from one array position to another in JavaScript?
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- How to convert Object's array to an array using JavaScript ?
- How to get the elements of one array which are not present in another array using JavaScript?
- How to check whether an array is subset of another array using JavaScript ?
- What is the difference between array_merge and array + array in PHP?
- How to merge the first index of an array with the first index of second array?
- ES6 | Array
- Difference between array() and [] in PHP
- How to get random value out of an array in PHP?
- How to re-index an array in PHP?
- PHP | array() Function
- PHP append one array to another
- RMS Value Of Array in JavaScript
- How to clone array in ES6 ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


