15.3 Advanced Array Methods (15 mins)
Introduction to Advanced Array Methods: JavaScript provides powerful methods for manipulating arrays. These methods allow you to extract portions of arrays, insert or remove elements, search for elements, and iterate over arrays.
Array Methods:
slice(): Returns a shallow copy of a portion of an array into a new array, without modifying the original array. You provide the start and (optional) end index.

Syntax:
array.slice(start, end)Example:
splice(): Changes the contents of an array by removing, replacing, or adding elements. It modifies the original array.

Syntax:
array.splice(start, deleteCount, item1, item2, ...)Example (Remove and Add Elements):
indexOf(): Returns the first index at which a specified element is found in the array. Returns-1if the element is not found.Syntax:
array.indexOf(element)Example:
forEach(): Executes a provided function once for each array element. It's useful for iterating over arrays.Syntax:
array.forEach(function(element, index, array))Example:
Student Activity (15 mins):
Objective: Students will practice using advanced array methods to extract, modify, find, and iterate over arrays.
Step-by-Step Activity:
Create an HTML file (e.g.,
arrays_advanced_methods.html) and write JavaScript code that demonstrates the use of advanced array methods.Example Code:
Run the file in a browser:
Open the HTML file in a browser and use Developer Tools (F12 or right-click and select Inspect > Console) to view the results of the array methods.
Activity:
Step 1: Use
slice()to extract the first 3 elements from an array and print the result.Step 2: Use
splice()to replace an element in an array with two new elements.Step 3: Use
indexOf()to find the position of a specific element in an array.Step 4: Use
forEach()to iterate through an array and print each element and its index.
Challenge:
modify the code to show the results in a <div id="Result"> in index.html and add a "Next" button to carry out the step-by-step processing of the `console.log` outputs, you can follow these steps:
Add a
<div>element with theid"Result" to display the results.Add a "Next" button to trigger the display of each step.
Modify the JavaScript to handle the button click and display each result sequentially.
Here's the modified code:
JavaScript Logic:
Created an array
resultsto store all the output strings.Used a
currentIndexto keep track of which result to display next.Added an event listener to the "Next" button to display the next result in the
resultsarray each time it's clicked.Disabled the button once all results have been displayed.
Write a function that finds the first occurrence of a number greater than 10 in an array and returns its index using
indexOf()andsplice().
Activity Follow-up Questions:
How does
slice()differ fromsplice()when working with arrays?What advantage does
forEach()offer over a traditionalforloop when iterating over arrays?In what scenarios would you use
indexOf()to search for elements in an array?
Expected Outcome:
Students will:
Understand how to use advanced array methods like
slice(),splice(),indexOf(), andforEach().Practice extracting and modifying elements in arrays using
slice()andsplice().Learn how to search for elements in arrays using
indexOf()and how to iterate over arrays usingforEach().
Last updated