jQuery.grep(array, function(element, index) [, invert])
<!DOCTYPE html>
<html>
<head>
<title> jQuery grep() method </title>
<script src = "/ajax/libs/jquery/3.5.1/jquery.min.js"> </script>
</head>
<body>
<h4> This is an example of using the grep() method. </h4>
<p> Click the following button to see the effect. </p>
<button id = "btn"> Click me </button>
<p id = "p1"> </p>
<p id = "p2"> </p>
<script>
$(document).ready(function() {
$("#btn").click(function(){
var arr = [ 12, 20, 60, 43, 56, 41, 60, 19, 42, 60, 78, 18];
$("#p1").html("Array elements before applying the <b> grep() </b> method: " + arr + "<br>");
arr = $.grep(arr, function(element, index) {
return ( element%2 == 0 && index >= 2 );
});
$("#p2").html("Array elements after applying the <b> grep() </b> method: " + arr);
});
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title> jQuery grep() method </title>
<script src = "/ajax/libs/jquery/3.5.1/jquery.min.js"> </script>
</head>
<body>
<h4> This is an example of using the grep() method. </h4>
<p> Click the following button to see the effect. </p>
<button id = "btn"> Click me </button>
<p id = "p1"> </p>
<p id = "p2"> </p>
<script>
$(document).ready(function() {
$("#btn").click(function(){
var arr = [ 12, 20, 60, 44, 56, 41, 60, 19, 42,];
$("#p1").html("Array elements before applying the <b> grep() </b> method: " + arr + "<br>");
arr = $.grep(arr, function(element, index) {
return ( element != 60);
}, true);
$("#p2").html("Array elements after applying the <b> grep() </b> method: " + arr);
});
});
</script>
</body>
</html>