Jquery Grep Example - Jquery

How to use grep function in jquery?

Snippet Code


  
Rate this page :
  [ 0 votes]

This sample code allows you to filter and remove elements in an array. Here function $.grep() is used to filter elements.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script type='text/javascript'> (function($) { $(document).ready(function() { var months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; months = $.grep(months, function(value, i) { return ( value.indexOf('M') == 0 ); }); $('#output').html( '<li>' + months.join('</li><li>') + '</li>' ); }); })(jQuery); </script> <div id='output'></div>

Tags


Ask Questions

Ask Question