Let’s start with the general business requirements, which are to retain data that meets certain criteria from the data returned from the back end. The requirement, reduced to code, is to keep qualifying elements in an array.
Simulate the above requirement by reserving elements less than or equal to 4 from the array [1,2,5,6,8,9]. Loop through the array, and then check from the loop if the element is greater than 4, then use splice to weed it out.
The code is as follows:
And then we’re like, why are 6 and 9 preserved? That’s because every time we find an element greater than 4, we just splice it out, and notice that splice is a mutating method, which is a method that changes the array, so when I is equal to 2, we find the element 5, and then when we splice, because there’s a void after the element 5 is deleted, The following elements’ 6 ‘, ‘8’ and ‘9’ are added one bit ahead, but the value of I is directly jumped to the position of element ‘8’ after the loop is completed by I ++ statement. Element ‘6’ escapes the bullet because of the complement. And then the loop goes back to 9, and it’s also avoided because of the replacement, so it’s 1269
The general process is shown in the figure below:
The same thing happens when I is equal to 4, the element ‘9’ fills in and dodges a bullet.
So in a judgment statement we can go like this
When using splice, pay attention to array changes and index positions.
We can also use a new array to store the elements that match the criteria, or we can use the array filter method to filter the elements
In this paper, the…
PS:
Due to a lot of projects at the end of the year, the author worked hard to work overtime and produced an APP alone in a month, so it was delayed. I am very sorry for that. Whenever I have free time, I will share with you some practical practical demo of my work.