All language subtitles for 012 Arrays & Array Methods like map()_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:02,220 --> 00:00:03,810 Now besides objects, 2 00:00:03,810 --> 00:00:08,039 another crucial JavaScript value type are arrays. 3 00:00:08,039 --> 00:00:11,010 Though, technically, arrays are objects, 4 00:00:11,010 --> 00:00:14,040 but they are special kinds of objects. 5 00:00:14,040 --> 00:00:17,490 Now, an array is created by using square brackets, 6 00:00:17,490 --> 00:00:20,010 opening and closing square brackets. 7 00:00:20,010 --> 00:00:22,410 And the idea behind an array 8 00:00:22,410 --> 00:00:27,060 simply is that you can create a list of values. 9 00:00:27,060 --> 00:00:30,660 So where objects allow you to group values together 10 00:00:30,660 --> 00:00:32,820 with key-value pairs, 11 00:00:32,820 --> 00:00:36,720 the idea behind arrays is to have just values, 12 00:00:36,720 --> 00:00:39,030 which are stored in a certain order 13 00:00:39,030 --> 00:00:43,470 and which can be accessed by their position in that list. 14 00:00:43,470 --> 00:00:46,230 So for example here, if I have an array of hobbies, 15 00:00:46,230 --> 00:00:50,260 let's say Sports, Cooking, and Reading, 16 00:00:53,010 --> 00:00:55,160 if I have such an array, I can access the array values 17 00:00:55,160 --> 00:01:00,160 by using square brackets after the name of the variable 18 00:01:01,440 --> 00:01:03,930 or constant that stores my array. 19 00:01:03,930 --> 00:01:06,063 So by using these square brackets here. 20 00:01:07,050 --> 00:01:10,850 And then there, I use the index, as it's called, 21 00:01:10,850 --> 00:01:13,653 of the element I want to output in this case. 22 00:01:14,550 --> 00:01:16,890 Now the index starts at zero. 23 00:01:16,890 --> 00:01:19,500 So Sports here has an index of 0, 24 00:01:19,500 --> 00:01:22,850 Cooking has an index of 1, Reading has an index of 2. 25 00:01:23,730 --> 00:01:25,170 If I wanna output Sports here, 26 00:01:25,170 --> 00:01:29,220 I would dare for access hobbies, square brackets, 0. 27 00:01:29,220 --> 00:01:32,820 With that, if we reload this and take a look at the console, 28 00:01:32,820 --> 00:01:34,203 I would see Sports. 29 00:01:36,210 --> 00:01:40,020 Now arrays are a super common value type and JavaScript 30 00:01:40,020 --> 00:01:42,990 because it turns out that you quite often 31 00:01:42,990 --> 00:01:45,870 need to store a list of values. 32 00:01:45,870 --> 00:01:48,840 For example, in this demo React application 33 00:01:48,840 --> 00:01:50,820 I showed you earlier in the course, 34 00:01:50,820 --> 00:01:53,460 there also was a content array, 35 00:01:53,460 --> 00:01:57,810 which actually contained more arrays as values 36 00:01:57,810 --> 00:02:00,180 because that's all important to know, 37 00:02:00,180 --> 00:02:03,990 arrays can contain any kinds of values. 38 00:02:03,990 --> 00:02:08,130 They can contain other arrays, objects, numbers, strings, 39 00:02:08,130 --> 00:02:09,300 whatever you want. 40 00:02:09,300 --> 00:02:12,450 Here I have an array of more arrays, 41 00:02:12,450 --> 00:02:17,450 and those inner nested arrays then actually contain strings. 42 00:02:18,930 --> 00:02:22,380 Now when defining array, the individual values 43 00:02:22,380 --> 00:02:26,130 that make up the array are separated by commas. 44 00:02:26,130 --> 00:02:30,420 And then as mentioned, you access values by their index. 45 00:02:30,420 --> 00:02:33,900 Now since arrays are such a super important, 46 00:02:33,900 --> 00:02:36,900 very common value type in JavaScript, 47 00:02:36,900 --> 00:02:40,860 there actually also are a couple of built-in utility methods 48 00:02:40,860 --> 00:02:44,220 you can use on arrays to transform 49 00:02:44,220 --> 00:02:47,610 or read the values stored in the arrays. 50 00:02:47,610 --> 00:02:50,820 You can access these utility array methods 51 00:02:50,820 --> 00:02:54,540 by using the array name and then the dot notation. 52 00:02:54,540 --> 00:02:57,060 And then you should automatically get a suggestion 53 00:02:57,060 --> 00:02:59,760 of the methods that are available here. 54 00:02:59,760 --> 00:03:02,190 For example, there is the push method, 55 00:03:02,190 --> 00:03:05,130 which allows you to add a new item to an array. 56 00:03:05,130 --> 00:03:07,020 For example, Working. 57 00:03:07,020 --> 00:03:11,040 If I add this here and I then console.log hobbies, 58 00:03:11,040 --> 00:03:12,450 you will see that now, 59 00:03:12,450 --> 00:03:15,840 hobbies here actually contains four elements, 60 00:03:15,840 --> 00:03:17,910 therefore ending with the index 3, 61 00:03:17,910 --> 00:03:20,073 since we start at the index 0. 62 00:03:20,910 --> 00:03:24,450 And the last element is this Working string 63 00:03:24,450 --> 00:03:27,360 I just pushed onto the array. 64 00:03:27,360 --> 00:03:29,460 Now there, of course, are way more methods, 65 00:03:29,460 --> 00:03:31,470 and I'm not going to explain them all here. 66 00:03:31,470 --> 00:03:33,870 But, for example, a never method, which you should know, 67 00:03:33,870 --> 00:03:36,030 which can be quite useful sometimes, 68 00:03:36,030 --> 00:03:38,880 is the findIndex method. 69 00:03:38,880 --> 00:03:42,360 This is a method that allows you to find the index 70 00:03:42,360 --> 00:03:43,563 of a certain value. 71 00:03:44,580 --> 00:03:48,990 For this, findIndex actually takes a function as an input, 72 00:03:48,990 --> 00:03:52,350 and that's a great use case for using such a arrow function, 73 00:03:52,350 --> 00:03:54,210 since it's super short to write 74 00:03:54,210 --> 00:03:56,520 and not a lot of characters to type. 75 00:03:56,520 --> 00:03:59,520 This arrow function, which you pass to findIndex 76 00:03:59,520 --> 00:04:02,940 should accept at least one input parameter, 77 00:04:02,940 --> 00:04:05,010 which could be called item, 78 00:04:05,010 --> 00:04:07,410 because findIndex behind the scenes 79 00:04:07,410 --> 00:04:09,450 will execute this function, 80 00:04:09,450 --> 00:04:11,760 which you're passing to findIndex, 81 00:04:11,760 --> 00:04:14,250 and will automatically provide a value 82 00:04:14,250 --> 00:04:16,230 for this input parameter. 83 00:04:16,230 --> 00:04:19,019 Therefore, of course, your function also must accept it 84 00:04:19,019 --> 00:04:21,899 in order to then use it in this function body. 85 00:04:21,899 --> 00:04:25,110 Now, in this function body, in case of findIndex, 86 00:04:25,110 --> 00:04:29,040 you then should return true if you have the item 87 00:04:29,040 --> 00:04:32,940 that you were looking for, and false otherwise. 88 00:04:32,940 --> 00:04:37,940 For that, we can return item === 'Sports', For example, 89 00:04:38,970 --> 00:04:43,230 if we were looking for the index of the item Sports. 90 00:04:43,230 --> 00:04:45,750 So if we were looking for the index of this item. 91 00:04:45,750 --> 00:04:47,760 What this code here does is 92 00:04:47,760 --> 00:04:51,990 it executes this function automatically behind the scenes 93 00:04:51,990 --> 00:04:56,070 for every item in this array, including this Working item, 94 00:04:56,070 --> 00:04:58,680 which was pushed onto the array. 95 00:04:58,680 --> 00:05:01,950 And then for every item, it compares that item. 96 00:05:01,950 --> 00:05:04,710 So that text here to this text, 97 00:05:04,710 --> 00:05:06,540 with help of this comparison operator, 98 00:05:06,540 --> 00:05:07,803 which I mentioned before. 99 00:05:09,090 --> 00:05:13,500 If the two are equal, this function here returns true, 100 00:05:13,500 --> 00:05:15,360 and therefore findIndex knows 101 00:05:15,360 --> 00:05:17,730 that I found the item I was looking for, 102 00:05:17,730 --> 00:05:21,510 and it will then give me the index of that item. 103 00:05:21,510 --> 00:05:23,490 Otherwise, this will return false, 104 00:05:23,490 --> 00:05:26,043 and findIndex will do nothing. 105 00:05:27,180 --> 00:05:30,670 So, therefore, here I could store that index in a constant 106 00:05:32,910 --> 00:05:34,443 and then console.log it. 107 00:05:35,790 --> 00:05:39,060 If I save that here, I get 0 108 00:05:39,060 --> 00:05:40,710 because indeed Sports, 109 00:05:40,710 --> 00:05:44,100 which is the item I'm looking for here, has the index 0 110 00:05:44,100 --> 00:05:47,070 since it's the first item in that array. 111 00:05:47,070 --> 00:05:48,840 Now this code can definitely be 112 00:05:48,840 --> 00:05:51,210 a bit more challenging to understand 113 00:05:51,210 --> 00:05:54,180 since we're passing a function to another function, 114 00:05:54,180 --> 00:05:56,850 and that's also something i'll get back to a little bit 115 00:05:56,850 --> 00:05:59,010 later in this section again. 116 00:05:59,010 --> 00:06:01,170 But in the end, all that's happening here 117 00:06:01,170 --> 00:06:03,780 is that findIndex needs a function, 118 00:06:03,780 --> 00:06:06,510 which it can execute on our behalf, 119 00:06:06,510 --> 00:06:08,340 and it will execute this function 120 00:06:08,340 --> 00:06:10,950 for every item in this array. 121 00:06:10,950 --> 00:06:14,460 It will pass that item for every execution 122 00:06:14,460 --> 00:06:16,230 into that function. 123 00:06:16,230 --> 00:06:18,570 And, therefore, of course, item will be different 124 00:06:18,570 --> 00:06:19,860 for every execution. 125 00:06:19,860 --> 00:06:22,470 It will be those items here. 126 00:06:22,470 --> 00:06:24,840 And then we compare the item we're getting 127 00:06:24,840 --> 00:06:27,750 to some value we are looking for. 128 00:06:27,750 --> 00:06:30,210 So this value we're looking for, of course, 129 00:06:30,210 --> 00:06:32,613 is defined by us, the developer. 130 00:06:33,870 --> 00:06:36,213 That's how this function here works. 131 00:06:37,140 --> 00:06:39,540 Now as a side note, if you have code like this, 132 00:06:39,540 --> 00:06:43,470 an arrow function where all you do in the function body, 133 00:06:43,470 --> 00:06:47,010 so between these curly braces, is return something, 134 00:06:47,010 --> 00:06:49,260 you can even shorten this code 135 00:06:49,260 --> 00:06:51,210 and get rid of the return statement 136 00:06:51,210 --> 00:06:54,090 and get rid of these curly braces, 137 00:06:54,090 --> 00:06:56,340 and write this code like this. 138 00:06:56,340 --> 00:06:58,800 Also get rid of this semicolon here. 139 00:06:58,800 --> 00:07:02,190 This is then the shortest possible way of writing this. 140 00:07:02,190 --> 00:07:05,370 And another advantage of using such a arrow function, 141 00:07:05,370 --> 00:07:08,640 because this is now a very short, concise piece of code 142 00:07:08,640 --> 00:07:12,210 for defining a function that takes an input called item, 143 00:07:12,210 --> 00:07:14,010 and then compares that input 144 00:07:14,010 --> 00:07:17,520 to some other value to yield true if these are equal 145 00:07:17,520 --> 00:07:19,113 or false, otherwise. 146 00:07:20,220 --> 00:07:22,710 Now, another very useful utility method 147 00:07:22,710 --> 00:07:26,793 you'll see a lot throughout this course is the map method. 148 00:07:27,960 --> 00:07:32,520 Map allows you to transform every item in an array 149 00:07:32,520 --> 00:07:33,780 to another item. 150 00:07:33,780 --> 00:07:36,450 for that map, just like findIndex, 151 00:07:36,450 --> 00:07:38,460 takes a function as an input, 152 00:07:38,460 --> 00:07:40,170 typically such an arrow function 153 00:07:40,170 --> 00:07:43,713 since this is such a short and concise way of writing this. 154 00:07:44,550 --> 00:07:47,160 And like this arrow function for findIndex, 155 00:07:47,160 --> 00:07:48,660 this arrow function here 156 00:07:48,660 --> 00:07:53,310 also will receive every item in the array as an input 157 00:07:53,310 --> 00:07:56,970 because this function also will be executed automatically 158 00:07:56,970 --> 00:08:00,450 by map for every item in the array, 159 00:08:00,450 --> 00:08:03,180 and every item off the array will then be provided 160 00:08:03,180 --> 00:08:06,573 as an input value to this function when it's being executed. 161 00:08:07,772 --> 00:08:10,350 And then here you return the value 162 00:08:10,350 --> 00:08:12,663 this item should be transformed to, 163 00:08:13,620 --> 00:08:17,480 For example, item + "!". 164 00:08:20,040 --> 00:08:23,370 So that I add an exclamation mark to every string here. 165 00:08:23,370 --> 00:08:25,770 This of course is just a dummy example. 166 00:08:25,770 --> 00:08:27,390 But when working with React, 167 00:08:27,390 --> 00:08:29,940 you'll use this map method a lot 168 00:08:29,940 --> 00:08:32,400 for outputting list content, 169 00:08:32,400 --> 00:08:35,940 for mapping data into such JSX elements, 170 00:08:35,940 --> 00:08:39,030 but that is something I'll get back to later again. 171 00:08:39,030 --> 00:08:41,850 Here I'm just mapping my items to strings 172 00:08:41,850 --> 00:08:44,340 with an exclamation mark at the end. 173 00:08:44,340 --> 00:08:49,340 And what map will do is it'll not edit the original array. 174 00:08:50,040 --> 00:08:53,550 Instead, that array will stay unchanged. 175 00:08:53,550 --> 00:08:56,193 And instead, map will return a new array, 176 00:08:57,030 --> 00:08:58,623 which I'll name editedHobbies. 177 00:09:00,900 --> 00:09:04,350 If I then console.log editedHobbies here 178 00:09:04,350 --> 00:09:06,540 and I reload this page, 179 00:09:06,540 --> 00:09:09,570 I, therefore, get my editedHobbies here, 180 00:09:09,570 --> 00:09:12,543 with all these exclamation marks at the end. 181 00:09:15,030 --> 00:09:16,380 So that's what map does. 182 00:09:16,380 --> 00:09:19,470 And since we'll see map quite a bit throughout this course, 183 00:09:19,470 --> 00:09:22,110 I definitely wanted to mention it here. 184 00:09:22,110 --> 00:09:24,060 Now, what's important to note about map 185 00:09:24,060 --> 00:09:27,180 is that you can really use it to transform any item 186 00:09:27,180 --> 00:09:29,550 to any other kind of item. 187 00:09:29,550 --> 00:09:32,850 For example, here, we don't have to convert our strings 188 00:09:32,850 --> 00:09:34,290 to new strings. 189 00:09:34,290 --> 00:09:37,473 Instead, I could also convert them to JavaScript objects. 190 00:09:38,370 --> 00:09:40,140 For that, I would create an object 191 00:09:40,140 --> 00:09:43,380 with opening and closing curly braces, 192 00:09:43,380 --> 00:09:46,020 which here, however, would have to be wrapped 193 00:09:46,020 --> 00:09:49,800 with parentheses since, otherwise, they would be treated 194 00:09:49,800 --> 00:09:53,220 as the parentheses that wrapped the function body. 195 00:09:53,220 --> 00:09:56,100 If, instead, wanna return a JavaScript object, 196 00:09:56,100 --> 00:09:59,040 I have to wrap the curly braces with parentheses. 197 00:09:59,040 --> 00:10:02,340 And this will tell JavaScript that these curly braces 198 00:10:02,340 --> 00:10:06,420 will not define the function body of this arrow function, 199 00:10:06,420 --> 00:10:08,790 but instead will define a new object 200 00:10:08,790 --> 00:10:11,400 returned by that arrow function. 201 00:10:11,400 --> 00:10:15,510 And then here, I could define any key of my choice. 202 00:10:15,510 --> 00:10:17,550 And, for example, store the item, 203 00:10:17,550 --> 00:10:19,620 which in this case will be such a string 204 00:10:19,620 --> 00:10:21,153 as a value for that key. 205 00:10:22,170 --> 00:10:25,920 Really, you can create any kinds of values here, 206 00:10:25,920 --> 00:10:30,240 any objects of any shape, numbers, strings, booleans, 207 00:10:30,240 --> 00:10:31,770 whatever you want . 208 00:10:31,770 --> 00:10:34,110 Here in this example, I'll, in the end, 209 00:10:34,110 --> 00:10:38,160 create a new array that's full of JavaScript objects, 210 00:10:38,160 --> 00:10:40,470 where every object has a text key, 211 00:10:40,470 --> 00:10:43,833 which value is the value from the old array. 212 00:10:45,120 --> 00:10:50,073 So if we save this code and reload this, 213 00:10:51,270 --> 00:10:54,600 I now have a bunch of objects in my array. 214 00:10:54,600 --> 00:10:56,460 And if I expand those, 215 00:10:56,460 --> 00:11:00,450 you see that I have this text key here, 216 00:11:00,450 --> 00:11:05,310 and the value then is the value I had before in my array. 217 00:11:05,310 --> 00:11:08,433 And that's, of course, the case for all these objects. 17381

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.