Turning around to Pim's answer, the correct way to do this (without jQuery) would be this:
Array.prototype.find = function(match) { return this.filter(function(item){ return typeof item == 'string' && item.indexOf(match) > -1; }); }
But really, if you do not use this function in several places, you can simply use the existing filter method:
var result = x.filter(function(item){ return typeof item == 'string' && item.indexOf("na") > -1; });
The RegExp version is similar, but I think it will create a little more utility:
Array.prototype.findReg = function(match) { var reg = new RegExp(match); return this.filter(function(item){ return typeof item == 'string' && item.match(reg); }); }
It provides the flexibility to let you specify a valid RegExp string.
x.findReg('a'); // returns all three x.findReg("a$"); // returns only "banana" since it looking for 'a' at the end of the string.
Shmiddty
source share