Callbacks

filters, sorting, map, reduce

Abel Sanchez and John R. Williams

Callback Pattern

A callback is a function that is passed as an argument to other code. The receiving code is expected to call back (execute) the argument when needed.

Filter

Applying a filter creates a new array with all elements that pass your function. The method takes a callback function.

Filtering Objects

Active Learning 1

  • Use your City of Boston data.
  • You will repeat the Overx exercise
  • Write a filter to return the
    number of jobs with salaries over X

Active Learning 2

  • Use your City of Boston data.
  • You will repeat the Rich exercise
  • select salaries over 250K
  • build objects for everyone
  • graph the resulting dataset

ForEach()

ForEach Exercise

Sort

The sort method takes a function and sorts the array. Your function needs to return positive, negative, or zero.

Sort Numbers

Active Learning 3

  • Use your City of Boston data.
  • You will repeat the Sort exercise
  • sort function
  • sort by salary
  • graph sorted data

Map

Reduce

The End