Skip to content

Instantly share code, notes, and snippets.

@JoaoVictor6
Created June 29, 2022 00:21
Show Gist options
  • Select an option

  • Save JoaoVictor6/5e4b619888a1250b312c142d1e32c56e to your computer and use it in GitHub Desktop.

Select an option

Save JoaoVictor6/5e4b619888a1250b312c142d1e32c56e to your computer and use it in GitHub Desktop.
Array.prototype.filterTwo = function(callback){
const pass = []
const notPass = []
for(let i = 0; i < this.length; i++){
if(callback(this[i], i, this)) {
pass.push(this[i])
}else {
notPass.push(this[i])
}
}
return [pass, notPass]
}
const test = [1,2,3,4,5,6,7,8,9,10]
const [even, odd] = test.filterTwo(v => v%2)
console.log('> even:', even)
console.log('> odd:', odd)
// > even: [ 2, 4, 6, 8, 10 ]
// > odd: [ 1, 3, 5, 7, 9 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment