Considerations for deleting elements in an array using the Splice function
Delete an element with a value of C:
<script>
var arr = new Array ();
Arr[0] = ' a ';
Arr[1] = ' B ';
ARR[2] = ' C ';
ARR[3] = ' C ';
ARR[4] = ' e ';
for (var i=0 i<arr.length; i++) {
if (arr[i] = = ' C ') {
arr.splice (i,1);
i=i-1; I must subtract 1, otherwise one of the following elements will be skipped
}
</script>
Note: If you use for (i in ARR), one of the following elements will always jump over
<script>
var arr = new Array ();
Arr[0] = ' a ';
Arr[1] = ' B ';
ARR[2] = ' C ';
ARR[3] = ' C ';
ARR[4] = ' e ';
For (i in arr) {
if (arr[i] = = ' C ') {
arr.splice (i,1);
i=i-1; This line of code is invalid and the value of I does not change
}
alert (arr.length);
}
</script>
This article discusses the use of the splice function in the array of elements to delete the note is small set to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.