We have all worked with jQuery. Sometimes we have also used jQuery arrays. Assume we have an array and in that array we need to exclude a specific element and create a new array without excluding that element. How to do that? We will use a for loop and just loop through the array, apply some conditions and if a criteria matches, we will do some operation. Here I will share you another way to do it, using a function called grep in jQuery. Normally the grep function acts as each in jQuery.
Please see this article in my blog here.
Backgroud
I am working on a client-side application where there are many client-side arrays and variables. We handle our entire client-side processes using jQuery. I usually go through the operation of finding and removing array elements using jQuery. I thought it would be better if I share some knowledge about it here. I hope it will be helpful for users.
Using the code
To start you need to include a jQuery reference.
- <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
Consider the following as our array.
- var myArray = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
Next, we will formulate this array to a HTML table, so that the visualization will be perfect. To do this we will have a function that will just convert the array elements to a HTML table. The following is our function body:
- function buildTable(array,message){
- var html='<table><caption>'+message+'</caption><tr>';
- for(var i=0;i<array.length;i++)
- {
- html+='<td>'+array[i]+'</td>';
- }
- html+='</tr></table>';
- $("#body").html(html);
- }
- <style>
- tr {
- border: 1px solid #ccc;
- }
- td {
- border: 1px solid #ccc;
- padding: 10px;
- }
- #body {
- margin: 30px;
- }
- #click {
- margin: 30px;
- cursor: pointer;
- }
- </style>
Now we need to call this function, right?
- var myArray = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
- var message="My Array Elements Before Removing";
- buildTable(myArray,message);
Style the HTML table by providing the preceding styles.
So our output will be as follows.

Now we need to fire a click event in which we will determine which element is to be excluded from the array and finally assign a new element set to our existing array.
- <a hrefe="#" id="click">Click To Remove</a>
- $("#click").click(function() {
- var message = "My Array Elements After Removing";
- var excludedElement = ['Thursday'];
- myArray = jQuery.grep(myArray, function(value) {
- return value != excludedElement;
- });
- buildTable(myArray, message);
- });
- var excludedElement = ['Thursday'];
Now jQuery.grep will return our new array with filtered data.
- return value != excludedElement;
And then we are calling our buildTable function to formulate our new array to a HTML table. Once we call it we will get output as follows.

Complete Code
- <html>
- <head>
- <title>Remove Elements From An Array using JQuery- Sibeesh Passion</title>
- <style>
- tr{
- border:1px solid #ccc;
- }
- td{
- border:1px solid #ccc;
- padding: 10px;
- }
- #body{
- margin: 30px;
- }
- #click{
- margin: 30px;
- cursor:pointer;
- }
- </style>
- <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
- </head>
- <body>
- <a hrefe="#" id="click">Click To Remove</a>
- <div id="body"></div>
- <script>
- $(document).ready(function(){
- var myArray = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
- var message="My Array Elements Before Removing";
- buildTable(myArray,message);
- $("#click").click(function(){
- var message="My Array Elements After Removing";
- var excludedElement = ['Thursday'];
- myArray = jQuery.grep(myArray, function(value) {
- return value != excludedElement;
- });
- buildTable(myArray,message);
- });
- });
- function buildTable(array,message){
- var html='
- <table>
- <caption>'+message+'</caption>
- <tr>';
- for(var i=0;i <array.length;i++)
- {
- html+='<td>'+array[i]+'</td>';
- }
- html+='
- </tr>
- < /table>';
- $("#body").html(html);
- }
- </script>
- </body>
- </html>
Demo
Please find the demo in jsfiddle here: Exclude or remove elements
Conclusion
I hope you liked this article. Please share with me your feedback. Thanks in advance.

Sibeesh VenuPosted Aug 7, 2015, 2:15 AM
Ankit Bansal Thank you
Sibeesh VenuPosted Aug 7, 2015, 2:15 AM
Sathish Kumar Thank you
Ankit BansalPosted Aug 7, 2015, 2:12 AM
nice...thanks for share..
Sathish KumarPosted Aug 6, 2015, 9:34 PM
Good info sibeesh
Sibeesh VenuPosted Aug 6, 2015, 1:08 AM
Debasis Saha Thank you
Sibeesh VenuPosted Aug 6, 2015, 1:08 AM
Santhakumar Munuswamy Thank you
Sibeesh VenuPosted Aug 6, 2015, 1:08 AM
Rakesh Chavda Thank you
Sibeesh VenuPosted Aug 6, 2015, 1:08 AM
Gopi Chand Thank you
Debasis SahaPosted Aug 6, 2015, 12:52 AM
Nice article..
Santhakumar MunuswamyPosted Aug 5, 2015, 11:41 PM
Good Article. Thanks for sharing
RakeshPosted Aug 5, 2015, 10:22 AM
Good one
Gopi ChandPosted Aug 5, 2015, 9:56 AM
Good going :)
Sibeesh VenuPosted Aug 5, 2015, 7:21 AM
Nilesh Jadav Thank you Buddy. You can call me Sibi :)
Nilesh JadavPosted Aug 5, 2015, 7:20 AM
This is nice one Sibeesh Venu sir
Sibeesh VenuPosted Aug 5, 2015, 6:31 AM
Upendra Pratap Shahi Thank you Buddy. You can call me Sibi :)
Upendra Pratap ShahiPosted Aug 5, 2015, 3:25 AM
Great blast for techies Sir
Sibeesh VenuPosted Aug 5, 2015, 3:21 AM
Rajeesh Menoth Thank you
Rajeesh MenothPosted Aug 5, 2015, 3:12 AM
Good One Sibeesh Venu