Last active
August 2, 2021 18:39
-
-
Save rostegg/33c9ef05211e152c351d71f29de5c7bf to your computer and use it in GitHub Desktop.
Return subbaray from array of target indexes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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