/js-array-methods

JavaScript Array methods

Primary LanguageJavaScriptOtherNOASSERTION

General Assembly Logo

JavaScript Array methods

Prerequisites

Introduction

We'll look at the array methods that allow us to test and transform arrays more simply and consistently.

Objectives

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.

Array Methods

We'll explore and implement proxies for a variety of the JavaScript Array methods.

Array

Demo

The forEach method iterates over all of the elements in an array.

Code along

The map method returns a new array containing a transformation of each element in this array.

Lab

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) {

};

Demo

The reduce produces a single value from operating on all the values in the array. It "reduces" many to one.

Code along

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.

Additional Resources

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.