Javascript Array filter(terms)

Description

Javascript Array filter(terms)


Array.prototype.filter = function(terms) {
  var list = this;
  function filter(list,terms){
    var results = [];
    list.forEach(function(item){
      if ( item.match(terms)) { results.push(item); }
    }); //from  w ww  . j  a  v  a 2 s . c  o  m
    return results;
  }
  return terms.reduce(filter,list);
};


var terms = ['p','c'];
var list = ['apple','orange','peach','tropical fruit'];
var results = list.filter(terms);
console.log(results); // ["peach", "tropical fruit"]



PreviousNext

Related