Js array implements a ruby-like iterator

Source: Internet
Author: User

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, we suddenly found that js Array Processing is really troublesome, and some code is a lot. Compared with the ruby iterator, it is really inferior, mainly because there are too many code to be written, maybe js has a special way to process arrays, but I really don't know it, but I really want to implement something similar to ruby for js myself, and it is not difficult to implement it, 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.