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 7, 2015, 10:04 PM EDT
Good article. Thanks for sharing
Shridhar SharmaPosted Aug 7, 2015, 1:50 PM EDT
good one :)
RakeshPosted Aug 7, 2015, 9:20 AM EDT
Good Explain
Pankaj Kumar ChoudharyTOP 100Posted Aug 7, 2015, 8:56 AM EDT
Nice One Sir...
Nilesh JadavPosted Aug 7, 2015, 5:45 AM EDT
This is very nice Sibeesh Venu sir
Sibeesh VenuTOP 100Posted Aug 7, 2015, 3:33 AM EDT
Neeraj Kumar Thank you
Neeraj KumarPosted Aug 7, 2015, 3:21 AM EDT
Nice
Sibeesh VenuTOP 100Posted Aug 7, 2015, 2:10 AM EDT
Manoj Kulkarni Thank you
Sibeesh VenuTOP 100Posted Aug 7, 2015, 2:10 AM EDT
Gopi ChandThank you
Sibeesh VenuTOP 100Posted Aug 7, 2015, 2:09 AM EDT
Rajeesh Menoth Thank you
Manoj KulkarniPosted Aug 7, 2015, 1:12 AM EDT
Nice article
Gopi ChandPosted Aug 7, 2015, 12:33 AM EDT
Nice one
Rajeesh MenothPosted Aug 7, 2015, 12:20 AM EDT
Good One