In this article we will see how to remove an array element by index. We all work with client-side arrays, right? What will you do if you need to remove an array element? It will be easy for you to do, if you know the index of the element that we need to delete from the array. Here we will explain that. We will provide an option to select the index and will take that value, then we will delete the array. Simple, right? I hope you will like this article.
Please see this article in my blog here.
Using the code
- <button id="loadAndShow">Load Array And Show</button><br /><br />
- <div id="divloadAndShow"></div><br /><br />
- <input type="number" id="number" /><br /><br />
- <button id="RemoveAndShow">Remove And Show</button><br /><br />
- <div id="divRemoveAndShow"></div><br /><br />
- <style>
- div {
- display: none;
- }
- </style>
- < script >
- var myJSON = [];
- function removeArrayElementByIndex(myArray, index) {
- myArray.splice(index, 1);
- $('#divRemoveAndShow').html('Removed from Array and JSON string is ').append(JSON.stringify(myArray)).show();
- myJSON = myArray
- }
- $(function() {
- $('#RemoveAndShow').hide();
- $('#number').hide();
- $('#loadAndShow').click(function() {
- $('#RemoveAndShow').show();
- $('#number').show().val(0);
- $('#divloadAndShow').html('Loaded to Array and JSON string is ').append('[{ "name": "2014", "level": 1 }, { "name": "4", "level": 2 }, { "name": "12", "level": 3 }]').show();
- myJSON = $.parseJSON('[{ "name": "2014", "level": 1 }, { "name": "4", "level": 2 }, { "name": "12", "level": 3 }]');
- });
- $('#RemoveAndShow').click(function() {
- removeArrayElementByIndex(myJSON, $('#number').val());
- });
- });
- < /script>
- function removeArrayElementByIndex(myArray, index) {
- myArray.splice(index, 1);
- $('#divRemoveAndShow').html('Removed from Array and JSON string is ').append(JSON.stringify(myArray)).show();
- myJSON = myArray
- }
- //This is the data we load
- [{ "name": "2014", "level": 1 }, { "name": "4", "level": 2 }, { "name": "12", "level": 3 }]
Complete code
- <!DOCTYPE html>
- <html>
- <head>
- <title>Remove an array element by its index</title>
- <script src="jquery-2.0.2.min.js"></script>
- <style>
- div {
- display: none;
- }
- </style>
- <script>
- var myJSON = [];
- function removeArrayElementByIndex(myArray, index) {
- myArray.splice(index, 1);
- $('#divRemoveAndShow').html('Removed from Array and JSON string is ').append(JSON.stringify(myArray)).show();
- myJSON = myArray
- }
- $(function () {
- $('#RemoveAndShow').hide();
- $('#number').hide();
- $('#loadAndShow').click(function () {
- $('#RemoveAndShow').show();
- $('#number').show().val(0);
- $('#divloadAndShow').html('Loaded to Array and JSON string is ').append('[{ "name": "2014", "level": 1 }, { "name": "4", "level": 2 }, { "name": "12", "level": 3 }]').show();
- myJSON = $.parseJSON('[{ "name": "2014", "level": 1 }, { "name": "4", "level": 2 }, { "name": "12", "level": 3 }]');
- });
- $('#RemoveAndShow').click(function () {
- removeArrayElementByIndex(myJSON, $('#number').val());
- });
- });
- </script>
- </head>
- <body>
- <button id="loadAndShow">Load Array And Show</button>
- <br />
- <br />
- <div id="divloadAndShow"></div>
- <br />
- <br />
- <input type="number" id="number" />
- <br />
- <br />
- <button id="RemoveAndShow">Remove And Show</button>
- <br />
- <br />
- <div id="divRemoveAndShow"></div>
- <br />
- <br />
- </body>
- </html>
Remove an array element by its index.

Remove an array element by its index.

Remove an array element by its index.

Remove an array element by its index.

Conclusion
I hope this article will be useful. Please share with me your feedback. Thanks in advance.

Santhakumar MunuswamyPosted Aug 8, 2015, 2:04 AM
Good article. Thanks for sharing
Shridhar SharmaPosted Aug 7, 2015, 5:50 PM
good one :)
RakeshPosted Aug 7, 2015, 1:20 PM
Good Explain
Pankaj Kumar ChoudharyPosted Aug 7, 2015, 12:56 PM
Nice One Sir...
Nilesh JadavPosted Aug 7, 2015, 9:45 AM
This is very nice Sibeesh Venu sir
Sibeesh VenuPosted Aug 7, 2015, 7:33 AM
Neeraj Kumar Thank you
Neeraj KumarPosted Aug 7, 2015, 7:21 AM
Nice
Sibeesh VenuPosted Aug 7, 2015, 6:10 AM
Manoj Kulkarni Thank you
Sibeesh VenuPosted Aug 7, 2015, 6:10 AM
Gopi ChandThank you
Sibeesh VenuPosted Aug 7, 2015, 6:09 AM
Rajeesh Menoth Thank you
Manoj KulkarniPosted Aug 7, 2015, 5:12 AM
Nice article
Gopi ChandPosted Aug 7, 2015, 4:33 AM
Nice one
Rajeesh MenothPosted Aug 7, 2015, 4:20 AM
Good One