15.3 Advanced Array Methods (15 mins)

  1. 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.

  2. 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 -1 if 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:

  1. Create an HTML file (e.g., arrays_advanced_methods.html) and write JavaScript code that demonstrates the use of advanced array methods.

  2. Example Code:

  3. 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.

  4. 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.

  5. 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:

  1. Add a <div> element with the id "Result" to display the results.

  2. Add a "Next" button to trigger the display of each step.

  3. Modify the JavaScript to handle the button click and display each result sequentially.

Here's the modified code:

  • JavaScript Logic:

    • Created an array results to store all the output strings.

    • Used a currentIndex to keep track of which result to display next.

    • Added an event listener to the "Next" button to display the next result in the results array 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() and splice().


Activity Follow-up Questions:

  1. How does slice() differ from splice() when working with arrays?

  2. What advantage does forEach() offer over a traditional for loop when iterating over arrays?

  3. 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(), and forEach().

  • Practice extracting and modifying elements in arrays using slice() and splice().

  • Learn how to search for elements in arrays using indexOf() and how to iterate over arrays using forEach().

Last updated