Skip to content

Instantly share code, notes, and snippets.

@nate-getch
Created September 6, 2017 21:36
Show Gist options
  • Save nate-getch/f542ef3f912865e0523a0983fce10492 to your computer and use it in GitHub Desktop.
Save nate-getch/f542ef3f912865e0523a0983fce10492 to your computer and use it in GitHub Desktop.
Extending Array object in js to filter odd and even elements of given array
const arr = [1,2,3,4,5,6,7,8];
Array.prototype.even = function(){
let r = [];
r = this.filter( v => v % 2 == 0 );
return r;
}
Array.prototype.odd = function(){
let r = [];
r = this.filter( v => v % 2 != 0 );
return r;
}
console.log(arr.even());
console.log(arr.odd());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment