Skip to content

Instantly share code, notes, and snippets.

@rostegg
Last active August 2, 2021 18:39
Show Gist options
  • Save rostegg/33c9ef05211e152c351d71f29de5c7bf to your computer and use it in GitHub Desktop.
Save rostegg/33c9ef05211e152c351d71f29de5c7bf to your computer and use it in GitHub Desktop.
Return subbaray from array of target indexes
/*
Example:
const arr = [1,2,3,4,5,7,8]
arr.subarrayFromIndexes([0,77,3,5,15])
*/
Array.prototype.subarrayFromIndexes = function (indexes) {
if (!Array.isArray(this))
throw "indexes must be array"
return indexes.reduce((acc, current) => {
return this[current] ? [...acc, this[current] ] : acc
}, [])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment