Last active
March 8, 2019 03:03
-
-
Save AndroxxTraxxon/39d501ea4cd3a74646f16e1dcf4e2879 to your computer and use it in GitHub Desktop.
A JS Array filter function returning a filtered array witha designated maximum length.
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
const truncatedFilter = (arr, length, filter) => { | |
length = (arr.length < length) ? arr.length : length; | |
let output = new Array(length) | |
let count = 0; | |
for(let item of arr){ | |
if(filter(item)){ | |
output[count] = item; | |
count++; | |
if(count === length){ | |
return output; | |
} | |
} | |
} | |
return output.slice(0, count); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment