Program to use JavaScript Array flat() method
let a = [[1,2,3],[4,5],[6,7]];
let b = a.flat();
console.log(b);
Output
[
1, 2, 3, 4,
5, 6, 7
]
Steps Description
- Declare two variables
a
andb
- Initialize two variables
a
andb
with value of stringsa = [[1,2,3],[4,5],[6,7]]
andb
- The
array flat()
method converts a nested array to a new array. - The value of
b
variable is displayed with the help of console.log inbuilt function
Program to use JavaScript Array flat() method
let c = [[1,2],[3,4,5,[6,7],8],[9,10]];
let d = c.flat(2);
console.log(d);
Output
[
1, 2, 3, 4, 5,
6, 7, 8, 9, 10
]
Steps Description
- Declare two variables
c
andd
- Initialize two variables
c
andd
with value of stringsc = [[1,2],[3,4,5,[6,7],8],[9,10]]
andd = c.flat(2)
- The
array flat()
method converts a nested array to a new array. - The value of
d
variable is displayed with the help of console.log inbuilt function