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 />
Add CSS
- <style>
- div {
- display: none;
- }
- </style>
Add the scripts
- < 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>
In the preceding code you can determine a function
removeArrayElementByIndex that accepts our array and the index as arguments.
- function removeArrayElementByIndex(myArray, index) {
- myArray.splice(index, 1);
- $('#divRemoveAndShow').html('Removed from Array and JSON string is ').append(JSON.stringify(myArray)).show();
- myJSON = myArray
- }
When you click on the button
loadAndShow, we will load the array and show the contents.
-
- [{ "name": "2014", "level": 1 }, { "name": "4", "level": 2 }, { "name": "12", "level": 3 }]
And if you click on the button
RemoveAndShow we will delete the array by passing the array and index to the function removeArrayElementByIndex.
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>
OutputRemove 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.