array.filter(callbackfn[, thisArg]);
引數說明:function f (value, index, ar) { high = Math.floor(Math.sqrt(value)) + 1; for (var div = 2; div <= high; div ++) { if (value % div == 0) { return false; } return true; } var a = [31,33,35,37,39,41,43,45,57,49,51,53]; var a1 = a.filter(f); console.log(a1); //31,37,41,43,47,53
var f = function (value) { if (typeof value !== 'number'){ return false; } else { return value >= this.min && value <= this.max; } } var a = [6, 12, "15", 16, "the", -12]; var obj = {min : 10, max : 20}; var r = a.filter(f, obj); console.log(r); //12,16
function f (value, index, ar) { if (index == 0) { return true; } else { return ar[index - 1] === " "; } } var a = "The quick brown fox jumps over the lazy dog."; var subset = [].filter.call(a, f); console.log(subset); //T,q,b,f,j,o,t,l,d