Skip to content

Instantly share code, notes, and snippets.

@Sidd27
Created September 25, 2019 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sidd27/f420d400906aa60059906abc6aafea53 to your computer and use it in GitHub Desktop.
Save Sidd27/f420d400906aa60059906abc6aafea53 to your computer and use it in GitHub Desktop.
Init+each+accumulate+push -> scan
Array.prototype.scan = function (callback, initialValue) {
const appendAggregate = (acc, item) => {
const aggregate = acc[acc.length-1] //get last item
const newAggregate = callback(aggregate, item)
return [...acc, newAggregate]
}
const accumulator = [initialValue]
return this.reduce(appendAggregate, accumulator)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment