$.grep查找满足过滤功能数组元素[原始数组不受影响]

 

代码
<!DOCTYPE html>
<html>
<head>
<title>$.grep用法</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<script>
var arr = [ 1, 9, 3, 8, 6, 1, 5, 9, 4, 7, 3, 8, 6, 9, 1 ];

alert(
"原始数组:"+arr);

arr
= jQuery.grep(arr, function(n, i){ // i是索引 , n是数组元素 ,i 和 n 是相互对应的
//alert("n=" + n + ", i = " + i ) ;
return (n != 5 && i > 4);
});

alert(arr);
// 1, 9, 4, 7, 3, 8, 6, 9, 1

arr
= jQuery.grep(arr, function (a) {
//alert(a); // a对应元素
return a != 9;
});

alert(arr);
// 1, 4, 7, 3, 8, 6, 1
</script>
</body>
</html>

 

posted @ 2011-01-19 11:37  opqrst  阅读(176)  评论(0)    收藏  举报