Skip to content

Instantly share code, notes, and snippets.

@aquaductape
Last active June 19, 2019 21:40
Show Gist options
  • Save aquaductape/700daab31b65b46c4524e7779a605977 to your computer and use it in GitHub Desktop.
Save aquaductape/700daab31b65b46c4524e7779a605977 to your computer and use it in GitHub Desktop.
recreate common array methods that take higher order functions

Array.prototype.*

recreate common array methods that take higher order functions


Example

  • Array.prototype.myForEach = ...
  • Object.defineProperty(Array.prototype, 'myForEach', { value: ...

1. forEach

Syntax

arr.forEach(callback(element [, index [, array]])[, thisArg]);

View code

2. filter

Syntax

var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])

View code

3. map

Syntax

var newArray = arr.map(callback(element[, index[, array]])[, thisArg])

View code

4. find

Syntax

var resultElement = arr.find(callback(element[, index[, array]])[, thisArg])

View code

5. some

Syntax

var resultBoolean = arr.some(callback(element[, index[, array]])[, thisArg])

View code

6. every

Syntax

var resultBoolean = arr.every(callback(element[, index[, array]])[, thisArg])

View code

7. reduce

Syntax

var resultSingleValue = arr.reduce(callback(accumulator, currentValue[, index[, array]]), [, initialValue])

View code

8. sort

Syntax

var resultSorted = arr.sort([compareFunction])

View code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment