Home > Web Front-end > JS Tutorial > $.grep() filter function in jQuery Array filtering

$.grep() filter function in jQuery Array filtering

高洛峰
Release: 2017-01-04 16:18:48
Original
1068 people have browsed it

<pre code_snippet_id="1998964" snippet_file_name="blog_20161122_1_1873395" name="code" class="html">var = $.grep([1,32,5,63,8,4,-8,-5,2,56,9,62,-6],function (n , i){ 
if(n>50){ 
return n; 
} 
}); 
console.log(test);
 

---->[63,56,62]
Copy after login

$.grep(array,callback,[invert]) has two parameters. The first parameter is the array to be filtered, the second parameter is the filtering condition, and the third parameter (optional) ) means retaining elements (true) or deleting elements (false).

var = $.grep([1,32,5,63,8,4,-8,-5,2,56,9,62,-6],function (n , i){ 
if(n>50){ 
return n; 
} 
},true); 
console.log(test); 
---->[63,56,62]
Copy after login
var = $.grep([1,32,5,63,8,4,-8,-5,2,56,9,62,-6],function (n , i){ 
if(n>50){ 
return n; 
} 
},false); 
console.log(test); 
---->[1,32, 5,8,4,-8,-5,2,9,-6]
Copy after login

The above is the $.grep() filter function array filtering in jQuery introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will promptly Reply to everyone. I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to $.grep() filter function and array filtering in jQuery, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template