We'll look at the array methods that allow us to test and transform arrays more simply and consistently.
By the end of this lesson, students should be able to:
- Write callbacks to pass to array methods
- Write functions to emulate array methods
- Write functions using array methods to add functionality.
We'll explore and implement proxies for a variety of the JavaScript Array methods.
The forEach method iterates over all of the elements in an array.
The map method returns a new array containing a transformation of each element in this array.
Write a function, mutate, that takes an array and a function as arguments and changes the array in place based on the return value of invoking transform.
const mutate = function mutate(array, transform) {
};
The reduce produces a single value from operating on all the values in the array. It "reduces" many to one.
The every method checks to see if all elements of an array meet some test. The function used for this should only return true
or false
. This type of function is often called a predicate.
The some and filter methods are often quite useful.
Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.