JS Array implements a ruby-like iterator _javascript technique

Source: Internet
Author: User
Tags array length

Divided into the following sections:

• Basic implementation

• Reference the original object in the iteration, or change the value of the array directly instead of returning a new array

• Pass an infinite number of arguments to the iteration

• Basic implementation
Today suddenly found that JS array processing is really troublesome, some code is a lot of, compared to Ruby's iterator to really less than many, mainly to write code too much, perhaps JS has a special way of handling arrays, I really do not know it, But I really want to give JS to implement a similar ruby-like iterator, and realize it is not difficult, then start to do it.

Really should be glad that JS is a dynamic language ah, if it is static language, to achieve a very inconvenient (don't say to me to redefine a inherited from the array of classes), but with JS to achieve a much simpler, directly to the array object plus a method can be as follows:


Well, the iterator has actually realized ... Very simple ah, yes, but this is really convenient for a lot of oh, look at the following usage will know:

First we define an array:


1: Usage One:

Iterations use each array element:


Disclaimer: This can be abbreviated as: Aa.each (function (val) {alert (val)}), and the following are the same, for clarity, separately written

The result is to pop up the value of each array element in turn, oh ah, it is so simple, do not write hateful for, if the code a lot of time, this saving will also save a lot of code amount OH

2: Usage Two:

Iterates over each array element and returns an array of processed OH:


How did it turn out? As you can imagine, the first pop-up is "1,2,3,4", then pop Up is "2,4,6,8", each element is processed, and returned as a new array, the amount of Oh, very simple function, if you think it is useless, well, or to write for the loop, if you like this writing, Well, there are a lot of improvements to be made, and it's for everyone to explore, so now it's enough for me to save a lot of code.

3: more:

You can also add more useful methods to the array, such as: Random scrambling order (chess game?) Wait, but I really hope that JS can include more punctuation in the method name, so that you can define each? or each! this cautionary method, but unfortunately

• Reference the original object in the iteration, or change the value of the array directly instead of returning a new array

I have an iterator here that can be said to be an improved version, because I suddenly have to refer to the original iteration object in the Func function, and make the following modifications


The changes are not large, they are compatible with all the features mentioned earlier, and they add references to the array objects


We can refer to the original array object in the Func function, and the index position of the current element, which can add many functions oh, first of all, you can directly modify the value of the original array,!!! But if you delete an array element in the Func function, there may be unexpected errors. !!! , let's do an experiment to see what happens:


Oh, we removed the element with a value of 2 in the Func function, and the result, in turn, pops up the 1,3,4,undefined, which is that the iterative function doesn't know that your array length has changed, and how do you solve the problem?


The initial idea is to determine whether the element is empty and, if it is empty, do nothing. The,&& operator will not perform any of the following statements if it is false. today found that this method is not considered , because I omitted to deal with an element, think about it, so you need to add an offset variable in the handler function to guide the current offset, each delete element will offset variable minus one, index to index+ Offset, which can solve the problem of deleting elements, but can not solve the problem of adding elements, so this iterator can not dynamically add array elements, otherwise there will be errors, pay attention to

Let's just stop here.

• Pass an infinite number of arguments to the iteration

Back again, this time we're going to allow this iteration to deliver more useful custom parameters than the system can customize, of course, all of the previous actions are compatible, and none of the modifications will compromise the previous operation's compatibility.

We modify the main method as follows:


OK, add a parameter, just a parameter, no fuss, but it can already complete all the parameters passed, see examples:


Or just the example, we want to delete an element in the array, we pass in the parameters to determine which value to delete, we use an object as a parameter to pass in, this object is a JSON object, oh yes, JSON can host any number of parameters, want to pass how much, not very simple, It seems that we have achieved a lot of functions, but not yet, more need to explore themselves.

Only use to expand IE foreach on the line, FF has been supported, and your writing seems to be very inefficient

Related Article

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.