Buy the full source code of application here:
Show More Show Less View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video we'll be looking at two
0:04
methods two array methods which is flat
0:07
and flat map so we'll be looking at
0:10
these two methods so flat method is
0:14
specifically used to actually flatten
0:16
out an array with the depth levels so
0:20
let me show you an example right here so
0:22
let me take an example of an array which
0:26
has some values 1 3 4
0:35
and we also have a nested array as well
0:38
so this is a nested
0:42
array this is actually the array that is
0:45
shown so we have this is actual outer
0:49
array that you
0:50
see this is the inner array the nested
0:53
array that you
0:55
see so now if you want to flatten this
0:58
array
1:00
into the same level if you want to just
1:03
split this nested
1:06
array so the output will be
1:09
135 and 356 78 so if you want to split
1:13
this up we use the flat method flatten
1:16
method so for doing this we can create a
1:19
new array we can
1:22
create
1:24
flat and then we can simply call this
1:27
method flat and here we need to Simply
1:30
pass the argument the actual array that
1:34
you want to flatten you are simply
1:35
passing it to this
1:37
function so if you just console log it
1:40
it actually flattens out the array and
1:43
if I console log it you will see all
1:46
this
1:50
array oh sorry we don't need to pass it
1:53
we are directly calling this if you see
1:56
we calling this on this array we don't
1:59
need to pass anything right here so by
2:01
default it takes the depth level so if
2:04
you don't pass anything it actually
2:06
takes this so if you you you can also
2:10
write like this but if you don't pass
2:12
any argument it takes one value as
2:14
default and now if you see it will flat
2:18
out the
2:20
array you will actually see the output
2:23
like this 13 5 35 678 so if you see this
2:28
it actually splitted up this nested
2:32
array it basically remove this
2:36
bracket and this is actually your output
2:39
135 356
2:43
78
2:45
so now we can even have nested array
2:48
inside this we can even have another
2:50
nested array
2:52
so we can
2:57
say so we can even do like this as as
3:00
well so if you see we have this outer
3:02
array this was our outer array this is
3:05
actually a nested
3:06
array inside this also we have another
3:09
nested array so you can see the depth
3:12
level is now two so depth level is now
3:15
two so if you
3:17
now execute the same method it will give
3:21
you this result it will not flatten out
3:24
this array this is
3:27
the this this is actually the array it
3:30
will only flatten out by one by default
3:33
if you don't pass anything in this
3:35
function it actually takes the value one
3:38
the default depth level so here need to
3:42
specifically pass the depth level that
3:44
you want to flatten on this array if you
3:47
now pass two it will first of all
3:49
flatten this
3:52
array and then it will also flatten this
3:55
array as well removing this now the
3:57
output will be 135 356 4578 so if you
4:02
closely look we have got this output
4:06
eight elements in this array and
4:07
everything has been flattened out so
4:10
this is actually the method guys flat
4:12
method which is used here we can provide
4:14
the depth level as well if you want to
4:17
flatten this array and now we'll be
4:19
looking at the method flat map flat map
4:23
is specifically
4:25
used if you see we have two methods one
4:29
is map and the second one is flat so
4:32
when you combine both these methods
4:34
together we get this method which is
4:37
flat
4:38
map so how we use this method basically
4:42
first of all if you want to map a method
4:46
let's suppose I
4:48
create result and now I need to map this
4:52
so you use first of all the map method I
4:54
will use so let me show you what this
4:57
method does it actually takes all the
5:01
arguments let me modify
5:05
this 1 2 3 4 5 so we actually take
5:13
individual element in this map function
5:15
we pass it in the num and we create a
5:18
new array so we basically multiply this
5:23
num by
5:26
two this is actually the logic we are
5:29
creating a new array using this map
5:31
function
5:32
providing 1 comma then 1 * by 2 1 comma
5:38
2 so it will give you this result if I
5:41
console log
5:44
it it will give you a flatten array if
5:47
you now see this array will not be
5:50
flattened because the flat method hasn't
5:53
been applied so you will actually see
5:56
this nested array 1 comma 2 1 comma 4 1A
5:59
6 1A 8 1A 10 so if you do need to
6:03
flatten this array as well with a single
6:05
step we then combine this using flat map
6:11
so what this function will become it
6:12
will become flat map so you write like
6:20
this so now it will give you this flat
6:23
method 1 2 1 4 1 16 1 18 110 exactly it
6:29
contain 10 elements so all the elements
6:32
have been flattened out in a single step
6:34
using this flat map function if you only
6:37
use the map function it will not flatten
6:39
out your array that's why we use flat
6:42
map function which combines the both the
6:44
functionalities of the flat function and
6:47
also it gives you the functionality of
6:50
the map function as well so this is a
6:53
difference between the two the flat and
6:56
the flat map if you also want to map the
6:58
elements you
7:00
combine that with flat map so these are
7:03
widely used array methods I personally
7:05
use them a lot inside applications this
7:08
is the actual thing how we use it so
7:10
thank you very much for watching this
7:12
video do check out my website as well
7:14
free mediat tools.com which contains
7:17
open-source video tools regarding audio
7:20
video and image and I will be seeing you
7:23
in the next video
#Programming
#Computer Education
