Home  >  Article  >  Web Front-end  >  How to use the $.grep() utility function in jQuery to filter elements in an array

How to use the $.grep() utility function in jQuery to filter elements in an array

黄舟
黄舟Original
2017-07-19 09:18:471393browse

How to use the $.grep() utility function in jQuery to filter elements in an array

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>使用$.grep()工具函数筛选数组中的元素</title>
<script src="../lib/jquery-2.0.3.min.js" type="text/javascript"></script>
<style type="text/css">
	body{font-size:13px}
	p{margin:5px;padding:10px;border:solid 1px #666;background-color:#666;width:300px;}
</style>
<script type="text/javascript">
	$(function(){
		var strTmp = "筛选前数据";
		var arrNum=[2,8,3,7,4,9,3,10,9,7,21];
		var arrGet = $.grep(arrNum,function(ele,index){
			return ele>5 && index<8;//元素值大于5且序号小于8
		});
		strTmp +=arrNum.join();
		strTmp +="<br/><br/>筛选后数据:";
		strTmp +=arrGet.join();
		$("#pTip").append(strTmp); 
	})
</script>
</head>
<body>
	<p id="pTip"></p>
</body>
</html>

The above is the detailed content of How to use the $.grep() utility function in jQuery to filter elements in an array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn