JavaScript deletes the corresponding element in an array by the element index number

  • 2020-05-17 04:47:18
  • OfStack

This example shows how JavaScript deletes the corresponding element in an array by the element index number. Share with you for your reference. The specific analysis is as follows:

JavaScript deletes elements from an array by their index number. If the third element is to be deleted, RemoveValByIndex(2) is used, and the JS array starts at 0


function RemoveValByIndex(arr, index) {
  arr.splice(index, 1);
}
test = new Array();
test[0] = 'Apple';
test[1] = 'Ball';
test[2] = 'Cat';
test[3] = 'Dog';
alert("Array before removing elements: "+test);
RemoveValByIndex(test, 2);
alert("Array after removing elements: "+test);

I hope this article has been helpful to your javascript programming.


Related articles: