Difference between revisions of "JavaScript/C4/Array-methods-in-JS/English"
Nancyvarkey (Talk | contribs) |
Nancyvarkey (Talk | contribs) |
||
(One intermediate revision by one other user not shown) | |||
Line 3: | Line 3: | ||
Author: Jayesh Katta Ramalingaiah | Author: Jayesh Katta Ramalingaiah | ||
− | Domain Reviewer: | + | Domain Reviewer: Ankita Maske |
− | Novice Reviewer: | + | Novice Reviewer: Praveeen S. |
Keywords: JavaScript, HTML, callback, parameter, method, loop | Keywords: JavaScript, HTML, callback, parameter, method, loop | ||
Line 24: | Line 24: | ||
Learning Objectives | Learning Objectives | ||
|| In this tutorial, we will learn about: | || In this tutorial, we will learn about: | ||
− | * Different '''Array Methods ''' | + | * Different '''Array Methods ''' |
* Advanced '''Array Loops '''in''' JS''' | * Advanced '''Array Loops '''in''' JS''' | ||
Line 46: | Line 46: | ||
|| | || | ||
* The files used in this tutorial are available in the''' Code files''' link on this tutorial page. | * The files used in this tutorial are available in the''' Code files''' link on this tutorial page. | ||
− | * Pls download and extract the | + | * Pls download and extract the files. |
* Make a copy and then use them for practising. | * Make a copy and then use them for practising. | ||
Line 149: | Line 149: | ||
|- | |- | ||
|| Press: Ctrl + S | || Press: Ctrl + S | ||
− | |||
− | + | Switch to '''Browser''' | |
− | + | || Save the file and switch back to the '''browser.''' | |
− | || | + | |
|- | |- | ||
Line 210: | Line 208: | ||
|- | |- | ||
|| Press: Ctrl + S | || Press: Ctrl + S | ||
− | |||
− | + | Switch to '''Browser''' | |
− | + | || Save the file and switch back to the '''browser.''' | |
− | || | + | |
|- | |- | ||
Line 270: | Line 266: | ||
|- | |- | ||
|| Press: Ctrl + S | || Press: Ctrl + S | ||
− | |||
− | + | Switch to '''Browser''' | |
− | + | || Save the file and switch back to the '''browser.''' | |
− | || | + | |
|- | |- | ||
Line 297: | Line 291: | ||
|| Only Narration | || Only Narration | ||
|| Let us take an example for each of these '''methods''' and understand them better. | || Let us take an example for each of these '''methods''' and understand them better. | ||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 319: | Line 309: | ||
− | If the '''array '''has the value '''2''', then it will '''return''' the ''' | + | If the '''array '''has the value '''2''', then it will '''return''' the '''indexOf 2.''' |
Line 326: | Line 316: | ||
|- | |- | ||
|| Press: Ctrl + S | || Press: Ctrl + S | ||
− | |||
− | + | Switch to '''Browser''' | |
− | + | || Save the file and switch back to the '''browser.''' | |
− | || | + | |
|- | |- | ||
Line 371: | Line 359: | ||
|- | |- | ||
|| Press: Ctrl + S | || Press: Ctrl + S | ||
− | |||
− | + | Switch to '''Browser''' | |
− | + | || Save the file and switch back to the '''browser.''' | |
− | || | + | |
|- | |- | ||
Line 409: | Line 395: | ||
|- | |- | ||
|| Press: Ctrl + S | || Press: Ctrl + S | ||
− | |||
− | + | Switch to '''Browser''' | |
− | + | || Save the file and switch back to the '''browser.''' | |
− | || | + | |
|- | |- | ||
Line 482: | Line 466: | ||
|- | |- | ||
|| Press: Ctrl + S | || Press: Ctrl + S | ||
− | |||
− | + | Switch to '''Browser''' | |
− | + | || Save the file and switch back to the '''browser.''' | |
− | || | + | |
|- | |- | ||
Line 507: | Line 489: | ||
|| Switch to Editor | || Switch to Editor | ||
|| Next, let’s learn about the '''map method'''. | || Next, let’s learn about the '''map method'''. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 544: | Line 523: | ||
const modifiedArr = arr.map((item) => item * 2); | const modifiedArr = arr.map((item) => item * 2); | ||
− | || | + | || I’m storing the modified '''array '''in the '''variable''' '''modifiedArr'''. |
|- | |- | ||
Line 554: | Line 533: | ||
|- | |- | ||
|| Press: Ctrl + S | || Press: Ctrl + S | ||
− | |||
− | + | Switch to '''Browser''' | |
− | + | || Save the file and switch back to the '''browser.''' | |
− | || | + | |
|- | |- | ||
Line 634: | Line 611: | ||
|- | |- | ||
|| Press: Ctrl + S | || Press: Ctrl + S | ||
− | |||
− | + | Switch to '''Browser''' | |
− | + | || Save the file and switch back to the '''browser.''' | |
− | || | + | |
|- | |- |
Latest revision as of 11:04, 21 June 2021
Title of the script: Array Methods
Author: Jayesh Katta Ramalingaiah
Domain Reviewer: Ankita Maske
Novice Reviewer: Praveeen S.
Keywords: JavaScript, HTML, callback, parameter, method, loop
|
|
Slide: Title | Hello and welcome to the spoken tutorial on “Array Methods in JS”. |
Slide:
Learning Objectives |
In this tutorial, we will learn about:
|
Slide: System Specifications | This tutorial is recorded using:
However, you may use any other browser of your choice. |
Slide : Pre-requisites | To practice this tutorial,
|
Slide: Code files |
|
Slide: Array Methods | To make development work easy, we can use many of the inbuilt array methods of JS.
|
Only Narration | Now, let us take an example for each of these methods and understand them better. |
Show VS editor | Open Visual Studio Code editor |
[Editor] Welcome Page -> Open Folder -> Practice-JS -> index.html & main.js
|
Then open the files index.html and main.js as explained in the earlier tutorials.
|
[Editor] Type:
<html lang="en"> <head> <title>Array Methods</title> </head> <body> <script src = "main.js" > </script> </body> </html> |
In the index.html file, replace the code as shown.
|
Press: Ctrl + S
|
Save the file and start the Live server. |
[Firefox] Press Ctrl + Shift + I
|
In the web browser, open the Browser developer tools panel and go to the Console tab. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
console.log(arr.length); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
Here, I have created a variable named arr and assigned an array with elements 1, 2 and 3. |
[Editor] Highlight:
|
And in the console, I am logging arr.length, which prints the length of the array. |
Press: Ctrl + S
Switch to Browser |
Save the file and switch back to the browser. |
[Browser] [Console Tab]:
Highlight: 3 |
Here we see 3.
That’s because there are 3 elements, and so the length of the array is 3.
|
Switch to Editor | Switch back to the editor. |
[Editor] Type:
const arr = [1, 2, 3]; console.log(arr.push(4));
|
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
I’m using the push method to add an element to the end of the array
|
[Editor] Highlight:
console.log(arr.push(4)); |
The push method returns the length of the array.
|
Press: Ctrl + S
Switch to Browser |
Save the file and switch back to the browser. |
[Browser] [Console Tab]:
Highlight: 4 |
In the console, we can see that the length of the array after pushing the value, is seen as 4. |
[Browser] [Console Tab]:
Highlight:
|
Observe here that the value 4 is added at the end of the array.
|
Switch to Editor | Next, let’s use the pop method. |
[Editor] Type:
console.log(arr.pop()); console.log(arr); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
Here, I used the pop method.
|
[Editor] Highlight:
console.log(arr.pop()); |
The pop method returns the deleted value in the array.
|
Press: Ctrl + S
Switch to Browser |
Save the file and switch back to the browser. |
[Browser] [Console Tab]:
Highlight: 3 |
In the console, we can see that the pop method has successfully removed the last element from the array.
|
Slide: Array Methods |
|
Only Narration | Let us take an example for each of these methods and understand them better. |
[Editor] Type:
console.log(arr.indexOf(2)); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
Here, I’m using the indexOf method and passing the value 2 to it.
|
Press: Ctrl + S
Switch to Browser |
Save the file and switch back to the browser. |
[Browser] [Console Tab]:
Highlight: 1 |
Since the element is present in the array, index 1 is returned and printed in the log.
|
Switch to Editor | Next, let’s see the unshift method. |
[Editor] Type:
arr.unshift(0); console.log(arr); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
Here, I’m using the unshift method and passing the value 0 to it.
|
Press: Ctrl + S
Switch to Browser |
Save the file and switch back to the browser. |
[Browser] [Console Tab]:
Highlight: Array(4) [ 0, 1, 2, 3 ] |
As you can see here, the value is added at the beginning of the array. |
Switch to Editor | Next, let’s see the shift method. |
[Editor] Type:
arr.shift(); console.log(arr); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
Here, I’m using the shift method, which deletes one value from the beginning of the array. |
Press: Ctrl + S
Switch to Browser |
Save the file and switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
|
Notice here that 1 is removed from the array.
|
Slide: Array Methods |
|
Only Narration | Let us look at an example for each of these methods and understand them better. |
[Editor] Type:
arr.forEach((item) => console.log(item)); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
Here, I’m using a forEach method to loop over the array.
|
[Editor] Highlight:
|
Here, I will pass any variable name as a parameter.
|
[Editor] Highlight:
|
On every iteration, I log the value. |
Press: Ctrl + S
Switch to Browser |
Save the file and switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
2 3 |
Look here. The array has looped over.
|
Switch to Editor | Next, let’s learn about the map method. |
[Editor] Type:
const modifiedArr = arr.map((item) => item * 2); console.log(modifiedArr); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
Here, I’m using the map method.
|
Slide: Map Method | map method is also very similar to forEach method.
|
[Editor] Highlight:
|
I’m storing the modified array in the variable modifiedArr. |
[Editor] Highlight:
console.log(modifiedArr); |
In the console, I’m logging the modifiedArr variable for verification. |
Press: Ctrl + S
Switch to Browser |
Save the file and switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
|
Observe the console.
|
Switch to Editor | Next, let's learn about the filter method. |
[Editor] Type:
const filteredArr = arr.filter((item) => item < 3); console.log(filteredArr); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
Here, I’m using the filter method.
|
[Editor] Highlight:
item < 3 |
Here, inside the callback function, I have mentioned a condition.
|
const filteredArr = | The returned values are stored in the variable filteredArr. |
[Editor] Highlight:
|
Here, I’m logging the filteredArr variable for verification. |
Press: Ctrl + S
Switch to Browser |
Save the file and switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
|
In the console, we have the filtered array.
|
Only Narration | With this, we have come to the end of this tutorial.
|
Slide: Summary
|
In this tutorial, we have learnt:
|
Slide:
[Highlight]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array |
We have covered only a few array methods in this tutorial.
|
Slide: Assignment | As an assignment:
|
Slide: About Spoken Tutorial Project |
|
Slide: Spoken tutorial workshops |
|
Slide: Forum questions | Pls post your timed queries in this forum |
Slide: Acknowledgement | Spoken Tutorial Project is funded by the Ministry of Education (MoE), Government of India |
Slide: Thanks | The script for this tutorial is contributed by Jayesh and this is Praveen signing off. Thank you for joining |