CSE 121B: JavaScript Language

Array Methods for Functional Programming

Overview

Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions or declarations instead of statements. Array methods that support functional programming include filter(), map(), and reduce(). There are other array methods that support functional programming, but this learning activity dives into these three common and useful methods.

Prepare

array.filter()

The array.filter() method creates a filtered array from the original array using the condition from the provided function.

array.map()

The array.map() method iterates through each element of the original array using a given function and produces a new array. The original array is not modified. The new array is returned by the map() method. The new array will have the same number of elements as the original array.

array.reduce()

The array.reduce() method is used to reduce the array to a single value. It executes a reducer function on each element of the array, resulting in a single output value. The reducer function takes four arguments: Accumulator, Current Value, Current Index, Source Array. The reducer function's returned value is assigned to the accumulator, whose value is remembered across each iteration throughout the array and ultimately becomes the final, single resulting value.

Check Your Understanding

For all of these exercises, use this array declaration:

let names = ['Nancy','Blessing','Jorge','Svetlana','Bob'];