Filter an array in javascript cross browser
arrays, javascript
Solution
May be you want this
if (!Array.prototype.filter) {
Array.prototype.filter = function(fun /*, thisp*/) {
var len = this.length >>> 0;
if (typeof fun != "function") {
throw new TypeError();
}
var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this) {
var val = this[i];
if (fun.call(thisp, val, i, this)) {
res.push(val);
}
}
}
return res;
};
}
var oldArr = ['one','two','3'];
newArr=oldArr.filter(function(a){
return !a.match(/\d/);
});
console.log(newArr);
Reference: MDN.
DEMO.
Problem
I have a little problem. i searched but did not get help to solve it. I have an array for example `var oldArr=['one','two','3'];` and i want to make a new array from this but only with string values and for that i am currently `using Array.filter` method and it is working but some one said that it is not supported in all browsers so my question is how can i filter my array with a cross browser solution. This is my current code ``` var oldArr=['one','two','3']; newArr=oldArr.filter(function(a){ return !a.match(/\d/); }); alert(newArr); ``` Thank you in advance for your effort.