Skip to content

Instantly share code, notes, and snippets.

@bitfishxyz
Created January 19, 2020 01:20
Show Gist options
  • Save bitfishxyz/50af5693afa768d9f6de95790ea2a1b6 to your computer and use it in GitHub Desktop.
Save bitfishxyz/50af5693afa768d9f6de95790ea2a1b6 to your computer and use it in GitHub Desktop.
const selfReduce = function (fn, initialValue) {
let arr = Array.prototype.slice.call(this)
let res
let startIndex
if (initialValue === undefined) {
for (let i = 0; i < arr.length; i++) {
if (!arr.hasOwnProperty(i)) continue
startIndex = i
res = arr[i]
break
}
} else {
res = initialValue
}
for (let i = ++startIndex || 0; i < arr.length; i++) {
if (!arr.hasOwnProperty(i)) continue
res = fn.call(null, res, arr[i], i, this)
}
return res
}
Array.prototype.selfReduce = selfReduce;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment