Javascript Array filter(p)
Array.prototype.filter = function(p) { console.log('custom filter called'); return this.reduce(function(acc, v) { if (p(v)) { acc.push(v);//from w ww. j a va2 s. co m } return acc; }, []); } var numbers = [ 1, 2, 3, 4 ]; var isEven = function(x) { return x % 2 == 0; } console.log('even numbers:', numbers.filter(isEven));
Array.prototype.filter = function(p) { var ret = []; for (var i = 0; i < this.length; i++) { var val = this[i]; var isTrue = p(val); if (isTrue) { ret.push(val);//from ww w .j ava2 s. c om } } return ret; }; function f(x) { return x % 2 == 0; } function g(x) { return x < 3; } var numbers = [ 1, 2, 3, 4 ]; console.log('numbers.filter(f):', numbers.filter(f)); // [ 2, 4 ] console.log('numbers.filter(g):', numbers.filter(g)); // [ 1, 2 ] console.log('numbers.filter(f).filter(g):', numbers.filter(f).filter(g)); // [ 2 ]