代码:
--------------------------------------------------------------------------------
代码如下 | 复制代码 |
function isBigEnough(element, index, array) { return (element >= 10); } var filtered = [12, 5, 8, 130, 44].filter(isBigEnough); |
// 12, 130, 44
结果:
--------------------------------------------------------------------------------
[12, 5, 8, 130, 44].filter(isBigEnough) : 12, 130, 44
完整实例
代码如下 | 复制代码 |
<script language="JavaScript" type="text/javascript"> if(!Array.prototype.filter) { Array.prototype.filter=function(fun) { var len=this.length; if(typeof fun!="function") throw new TypeError(); var res=new Array(); 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;};} function isBigEnough(element,index,array){return(element>=10);} |
时间: 2024-09-26 02:42:49