Monday, 22 April 2019

What is grep function in jquery

jQuery $.grep() Method. The jQuery method of $.grep() is used to filter the contents of an array

Example:- 

<html>
<head>
  <title>jQuery grep() function</title>
  <style>
  div {
    color: blue;
  }
  p {
    color: red;
    margin: 0;
  }

  </style>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<div></div>
<p></p>

<script>
var arr1 = [ 1, 7, 4, 8, 6, 1, 9, 5, 3, 7, 3, 8, 5, 8, 2 ];
$( "div" ).text( arr1.join( ", " ) );

arr1 = jQuery.grep(arr1, function( n, i ) {
  return ( n !== 5 && i > 6 );
});


$( "p" ).text( arr1.join( ", " ) );
</script>

</body>

</html>


Output: -

1, 7, 4, 8, 6, 1, 9, 5, 3, 7, 3, 8, 5, 8, 2
3, 7, 3, 8, 8, 2

No comments:

Post a Comment