Advanced array functions
Objectives
- use the Array.forEach function
- use the Array.every function
- use the Array.some function
- use the Array.map function
- use the Array.filter function
- use the Array.reduce function
- use the Array.sort function
Overview
The functions covered on this page take callback functions
as arguments. The callback functions are automatically passed
each element in the array. These functions could be implemented
using iteration (such as a for loop), but this syntax is more
concise.
- forEach(func): processes each element of array by sending it to func
- every(func): returns true if and only if each element of the array sent to func returns true
- some(func); returns true if any element of the array sent to func returns true
- map(func): returns an array consisting of each element of the original array processed by func
- filter(func): returns a new array consisting of only those elements which return true when sent to func
- reduce(func(previous, current, index, array), initial): computes a single value for an array using
initial value to start, and then computing a new value given the previous computed value to this point
and the current array element value
- sort(func(a,b)): uses func to determine the sort order; func is sent two array elements and is supposed
to return < 0 if the first element comes first, > 0 if the first element comes second, and 0
if the elements are equal
// set up array for use
var ar = ["Don","Ralph","Gale"];
// use forEach to display each item in the array
ar.forEach(function(a) { console.log(a); });
// displays:
// Don
// Ralph
// Gale
// check to see if 'a' is the second letter of EVERY array element
ar.every(function(a) { return a.charAt(1) == 'a'; });
// returns: false
// check to see if 'a' or 'o' is the second letter of EVERY array element
ar.every(function(a) { c = a.charAt(1); return c == 'a' || c == 'o'; });
// returns: true
// check to see if 'a' is the second letter of ANY array element
ar.some(function(a) { return a.charAt(1) == 'a'; });
// returns: true
// check to see if 'e' is the second letter of ANY array element
ar.some(function(a) { return a.charAt(1) == 'e'; });
// returns: false
// convert each element of the array to uppercase
ar.map(function(a) { return a.toUpperCase(); });
// returns: ["DON", "RALPH", "GALE"]
// filter out those elements which do not have 'a' as their second letter
// note the chaining of functions to display the result
ar.filter(function(a) { return a.charAt(1) == "a"; }).forEach(function(a) { console.log(a); });
// displays:
// Ralph
// Gale
// concatenates all the elements of the array
ar.reduce(function(pv, cv) { return pv + cv; });
// returns: "DonRalphGale"
// concatenates all the elements of the array and adds an initial value
ar.reduce(function(pv, cv) { return pv + cv; }, "Concatenated: ");
// returns: "Concatenated: DonRalphGale"
// computes a single value from all the array elements
// total = 1*1 + 2*2 + 3*3 + 4*4
[1,2,3,4].reduce(function(pv, cv, ndx) { return pv + cv * (ndx+1); });
// returns: 30
// set up array for sorting demonstrations
ar = [1,2,3,4,5,6,7,8,9,10];
// this comparison function sorts in decreasing order
ar.sort(function(a,b) { return b-a; });
// returns: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
// this comparison function gives a random order
ar.sort(function(a,b) { return [-1,1][Math.floor(Math.random() * 2)]; });
// returns: [5, 8, 2, 9, 7, 6, 10, 4, 3, 1]