Skip to content

Instantly share code, notes, and snippets.

@AndroxxTraxxon
Last active March 8, 2019 03:03
Show Gist options
  • Save AndroxxTraxxon/39d501ea4cd3a74646f16e1dcf4e2879 to your computer and use it in GitHub Desktop.
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.
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