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 with
       Â
the JavaScript?
   Â
</
title
>
    Â
ÂÂ Â Â Â
<
style
>
       Â
body {
           Â
text-align: center;
       Â
}
        Â
ÂÂ Â Â Â Â Â Â Â
h1 {
           Â
color: green;
       Â
}
   Â
</
style
>
</
head
>
Â
Â<
body
>
   Â
<
h1
>neveropen</
h1
>
    Â
ÂÂ Â Â Â
<
p
id
=
"GFG_UP"
></
p
>
    Â
ÂÂ Â Â Â
<
button
onClick
=
"GFG_Fun()"
>
       Â
click here
   Â
</
button
>
    Â
ÂÂ Â Â Â
<
p
id
=
"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
>
-
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 with
       Â
the JavaScript?
   Â
</
title
>
    Â
ÂÂ Â Â Â
<
style
>
       Â
body {
           Â
text-align: center;
       Â
}
        Â
ÂÂ Â Â Â Â Â Â Â
h1 {
           Â
color: green;
       Â
}
   Â
</
style
>
   Â
<
script
src
=
   Â
</
script
>
</
head
>
Â
Â<
body
>
   Â
<
h1
>neveropen</
h1
>
    Â
ÂÂ Â Â Â
<
p
id
=
"GFG_UP"
></
p
>
    Â
ÂÂ Â Â Â
<
button
onClick
=
"GFG_Fun()"
>
       Â
click here
   Â
</
button
>
    Â
ÂÂ Â Â Â
<
p
id
=
"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
>
-
Output: