Hi Guest

MobileUser

1 HomePage 1 Jobs 1 WalkIn 1Articles  

Cat Exam

 :) Latest Topic
AJAX
VB.NET
SQL Query
UDDI
CLASS
JavaScript Questions
SharePoint Interview
SilverLight
WCF
general knowledge
 ASP.NET
 :) Hot Jobs
 
 :) Latest Articles


  

 


Use of flat Array Method in JavaScript?
Question Posted on 02 Aug 2022

Home >> JavaScript >> JavaScript Questions >> Use of flat Array Method in JavaScript?




Use of flat Array Method in JavaScript?
We mainly use flat method basicaly to convert an original array into a new array. And this is done by collecting and concating the sub-arrays in an array into a single array. And one important fact about the flat array method is that we can compute the original into another array based on the depth of the array. And this method is really very useful because it makes computing the oeprations of an array much easier.

Example of flat() array method with syntax

(1)Syntax for flat array methods
array.flat(depth);

Here depth value is 1 by default and you can leave it empty. The depth value takes a number as its data type.

here array.flat(1) is equal to array.flat(). And array.flat(2) means depth above 2.

(2)Now flat() array method example 2
const arrExample=[1, 2, [3, 4, 5], 6, 7, 8];
arr.flat();


here expected output is
[1, 2, 3, 4, 5, 6, 7, 8]

in above array sub-array is [3,4,5].

Here is above example we use flat method to concatenate the two array together in a single array.

(3)Array method example 3
const arr2Example=[[[1, 2], 3, 4, 5]];
console.log(arr2Example.flat(2));


now output of above is
[1, 2, 3, 4, 5]

Here in above method we use depth value 2 to concatenate two array into a single array.
0
0



 
 Input Your Comments Or suggestions(If Any)
 Name
 Email(optional)
 Comments


Other Important Questions
regular expression for credit card like VISA, MASTER, AMEX
How to remove item from dropdownlist in Javascript
Different Data Types in Javascript in 2015?
syntax to get dropdownlist text in javascript
The snippet that has to be used to check if “a” is not equal to “null” is _________






 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |