When writing JS code, we often use the splice function to delete elements in the array, because the splice function modifies the arrays directly, eliminating the need to write an algorithm to move the other elements of the array to the deleted position. The splice feature is very powerful, in addition to deleting the elements of an array, you can also delete the use of adding new elements to the deleted location and so on. In this article, I'll just cover the use of splice's delete array element, and the pits I've encountered when using splice in a For loop, as a record lest I forget the pit next time.
Before using splice, the prerequisite is that you first have an array ...
var New Array (1, 2, 3, 4, 5); // initialize an array [1, 2, 3, 4, 5]
See this array, I think the middle of the number 3 is not good-looking, I want to delete it. The number 3 is the array subscript 2 element, and I just need to delete 1 numbers, so ...
var index = 2; // The number 3 subscript in the array var amount = 1; // deletes the number of digits from the position of the array index var num = arr.splice (index, amount); // deletes a number from the subscript 2 of the ARR array and assigns the deleted number to num
Wow, it's so easy to use splice to delete array elements, so I happily use the For loop inside ...
vararr =NewArray (1, 2, 3, 4, 5);//initializing a collection of numbersvarDelete_number = 3;//the number to be deleted //iterating through an array for(vari=0; i<arr.length; i++){ if(Arr[i] = = = Delete_number) {//If you find the array subscript that contains the number to be deleted varnum = Arr.splice (i, 1);//Delete 1 digits starting from I positionConsole.log ("successfully deleted" +num);//output The number that was deleted } Else{console.log (Arr[i]+ "not deleted");//if the array element of I subscript is not a number that needs to be deleted, the output number }}
Haha, the nasty number 3 must have been erased. But still need to look at the debugging information ...
See no, the number 3 was deleted!!! Haha, but look carefully, eh, 4??????? The lovely number 4 has not been looped through to ...
As mentioned earlier, splice is directly manipulating and modifying the array, so when the number 3 o'clock is found in the loop I subscript is 2, and when the number 3 is deleted, the array subscript i position saved in the number 4, and then to the next loop i is labeled 3 o'clock, the array subscript i position saved in the number is 5, So I skipped the number 4, so there's no cute number 4 in the debug message .... The principle is this, is not very around.
Digression: Because of my negligence, after adding splice to a For loop, one of the many NPCs in my H5 game project did not move to the appropriate position as expected. The most important thing is I also submitted to the Repository, and splice submitted hundreds of lines of code at the same time, so the fallback version can only be breakpoints debugging until late at night to find such a small error. So I wrote this article to record the use of splice pits in a for loop.
Said so much, how to solve the problem of missing the number 4??? Very simple, in the use of the next sentence of splice, change the value of the loop variable can ...
if (Arr[i] = = = Delete_number) { // if the array where the number to be deleted is located, the subscript var num = Arr.splice (i, 1); // Delete 1 digits starting from I position Console.log ("successfully deleted" +num); // output The number that was deleted = i-1; // Solution Solutions
After reading the above content, is not feel particularly simple and a piece of cake, wait, I want to make sure you do not like me splice caused by the low-level error, so please look at the following code, a little more complex than the above, if you only according to the code to launch the same answer as the result, you can ensure the next use splice will be conscious of avoiding the problem ...
vararr =NewArray (1, 2, 3, 4, 5);//initializing a collection of numbersvarDelete_number = 3;//the number to be deletedvarAmount = 2;////delete the number of digits from a position in the array varloop = Arr.length;//Number of Cycles //iterating through an array for(vari=0; I < loop; i++){ if(Arr[i] = = = Delete_number) {//If you find the array subscript that contains the number to be deleted varnum = Arr.splice (i, amount);//Delete 1 digits starting from I positionI= I-1;//change the loop variableloop = Loop-amount;//change the number of cycles } Else{console.log (Arr[i]+ ", "); }} //results: 1,2,5
JS's Splice () method uses the pits that may be encountered in the For loop