Code Monkey home page Code Monkey logo

js-array-methods's Introduction

General Assembly Logo

JavaScript Array Iteration Methods

Prerequisites

Objectives

By the end of this talk, developers 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.

Preparation

  1. Fork and clone this repository.
  2. Create a new branch, training, for your work.
  3. Checkout the training branch.
  4. Install dependencies with npm install.

Callbacks

A callback is a function that we pass as an argument to another function or method. This other function invokes the callback as a necessary part of accomplishing its task. The JavaScript Array Iteration Methods require callback functions.

We often use predicate functions, functions returning either true or false, as callbacks.

Arrow Functions

We frequently use arrow (sometimes referred to as fat arrow) functions as callbacks. This is convenient when the callback is anonymous.

Although the following is true of arrow functions:

> typeof () => {}
'function'

> () => {} instanceof Function
true

There are a few caveats.

Arrow functions:

  • cannot use arguments (trying generates: ReferenceError: arguments is not defined).
  • cannot be used as a Constructor (new does not bind this, no prototype property).
  • always have a lexically bound this (we'll learn more about that later).

Array Iteration Methods

We'll explore the array methods that allow us to test and transform arrays more simply and consistently, Iteration methods, and we'll model some of these JavaScript Array methods as functions.

We'll check our work in node or using bin/array-iteration-methods.js.

Demo: modeling and using forEach

The forEach method iterates over all of the elements in an array. Unlike a for loop, it cannot be stopped (all elements are processed). forEach returns undefined.

From the MDN documentation:

There is no way to stop or break a forEach() loop other than by throwing an
exception. If you need such behavior, the forEach() method is the wrong
tool, use a plain loop instead. If you are testing the array elements for a
predicate and need a Boolean return value, you can use every() or some()
instead. If available, the new methods find() or findIndex() can be used for
early termination upon true predicates as well.

This means that forEach is a poor choice for an operation on an array that may terminate early.

Code along: modeling and using reduce

The reduce method returns a single value from operating on all the values in the array. It "reduces" many to one. The original array does not change.

The key to using reduce properly is to methodically walk-through the "How reduce works" section at the above link.

Because reduce must access all of the elements of the array, we'll implement it in terms of forEach.

Code along: modeling and using map

The map method returns a new array the same size as the existing array. The elements of the new array are set to the return value of the callback passed to map invoked with the corresponding element from the original array as its argument (e.g. newArray[i] = callback(array[i])). The array map is called upon is not mutated.

Lab: modeling and using filter

The filter method returns a new array containing elements from the original array for which the callback returns true. The length of the new array may be 0, the callback returned false for every element, or equal to the length of the original array, the callback returned true for every element in the original array.

Callbacks passed to filter should be predicate functions.

Code along: modeling and using some

The some method return true if the callback returns true for any element of the array.

Callbacks passed to some should be predicate functions.

Lab: modeling and using every

The every method checks to see if all elements of the array meet some test. The function used for this should only return true or false. This type of function is often called a predicate.

Callbacks passed to every should be predicate functions.

Can we build every using some?

Code along: modeling and using find

The find method returns the first element in the array for which the callback returns true.

Could we have built some using find?

Lab: modeling and using findIndex

The findIndex method returns the index of the first element in the array for which the callback returns true.

Can we build findIndex using find?

Lab: modeling and using reduceRight

The reduceRight method ifs functionally equivalent to using reduce on the array returned by the reverse JavaScript Array method.

Hint: Start by writing forEachRight (similar to forEach but iterating from the last element (at index length-1) through the first (at index 0).

How would reduceRight differ from reduce?

Additional Resources

  1. All content is licensed under a CC­BY­NC­SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].

js-array-methods's People

Contributors

laurenfazah avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.