It can be divided into the following sections:
· Basic implementation
· Reference the original object in the iteration, or directly change the value of the array instead of returning a new array
· Input infinite parameters to Iteration
· Basic implementation
Today, I suddenly found that JS arrays are really troublesome to process,CodeSome of them are a bunch of things, which are really inferior to the ruby iterator, mainly because there is too much code to write. Maybe JavaScript has a special way to process arrays, I don't know, but I really want to implement a ruby-like iterator for JS myself, and it's not hard to implement it, so let's get started.
I should be glad that JS is a dynamic language. If it is a static language, it is inconvenient to implement it (not to mention re-define a class inherited from array ), however, it is much simpler to implement it with Js. Simply add a method to the array object, as shown below:
Well, the iterator has actually been implemented... It's easy, right, but it's a lot easier. Let's see the usage below:
First, we define an array:
1: usage 1:
Iteration uses each array element:
Statement: this can be abbreviated as AA. Each (function (VAL) {alert (VAL)});. The same is true for the following:
The execution result pops up the values of each array element in sequence. Oh, it's so simple, you don't have to write the hateful for. If there are a lot of code, this saves a lot of code.
2: usage 2:
Iteratively process each array element and return a processed array:
What are the results? As you think, the first pop-up is "1, 2, 3, 4", and then "2, 4, 6, 8". Each element is processed and returned as a new array, it's a very simple function. If you think it's useless, well, let's write the for loop. If you like this method, well, there are still many improvements, this is enough for me to save a lot of code.
3: more:
You can also add more useful methods to the array, such as randomly disrupting the order (board game ?) But I really hope that JS can include more punctuation marks in the method name, so that we can define each? Or each! This warning method is a pity
· Reference the original object in the iteration, or directly change the value of the array instead of returning a new array
I have an iterator that can be said to be the ultimate version, because I suddenly want to reference the original iteration object in the func function, so I made the following changes:
The changes are not big. It is also compatible with all the functions mentioned above and adds references to array objects.
We can reference the original array object and the index location of the current element in the func function, which can add many functions. First, we can directly modify the value of the original array,!!!However, if you delete an array element in the func function, unexpected errors may occur.!!!Next, let's make a test to see what will happen:
Oh, we deleted the element with the value of 2 in the func function. The result is 1, 3, 4, and undefined. That is, the iteration function cannot know that your array length has changed, how can we solve this problem after one loop?
The preliminary idea is to determine whether the element is null. If it is null, no operation will be performed. If it is false before the & operator, no subsequent statements will be executed.I found this method under consideration today.Because I missed an element and thought about it carefully, I need to add an offset variable to the processing function to guide the current offset. Each time an element is deleted, the offset variable is reduced by one, indexes are subject to index + offset, which can solve the problem of deleting elements, but cannot solve the problem of adding elements,Therefore, the array element cannot be dynamically added in this iterator. Otherwise, an error occurs. Note that
So far.
· Input infinite parameters to Iteration
Again, this time we want to allow this iteration to pass more useful custom parameters, rather than system-defined parameters. Of course, all the previous operations are compatible, all modifications do not affect the compatibility of the previous operations.
Modify the main method as follows:
OK. A parameter is added. It is just a parameter. There is nothing to worry about, but it can already complete all the parameter transfer. See the example below:
In the previous example, we want to delete an element in the array. We will use the passed parameters to determine which value to delete. We will use an object as a parameter to pass in, this object is a JSON object. ouye and JSON can carry any number of parameters. It is not easy to transfer as much as you want. It seems that we have implemented many functions. but it is not complete yet. More needs to be explored by yourself.
You only need to expand ie foreach. FF is supported, and your writing seems very inefficient.