Flatten and recursion of Array in JavaScript programs

Source: Internet
Author: User
How to use JavaScript to convert an Array like [, 3, [, [], [[[8]? The legendary ArrayFlatten. To solve this problem, we usually need Recursion to let the program loop by itself according to an algorithm. A book says, "recursion is a powerful programming technology." Okay, she doesn't

How to use JavaScript to convert an Array like [, 3, [, [], [[[8], 8? The legendary Array Flatten.

To solve this problem, we usually need Recursion to let the program loop by itself according to an algorithm. A book says, "recursion is a powerful programming technology." Well, she is not just JavaScript. Recursion can be difficult or simple (it is still difficult in general ). It should be appropriate to solve the above problem with recursion. This is a simple example of recursive use:

Flatten: function (ac ){
Var array = [];
Var group = this. arr;
If (ac) group = ac;
For (var I = 0; I <group. length; I ++ ){
If (group [I] instanceof Array ){
Array = array. concat (this. flatten (group [I]);
} Else {
Array = array. concat (group [I]);
}
}
Return array;
}

If (group [I] instanceof Array), call the function itself and perform recursion by passing parameters. Just when refactoring Array. js, I thought that since it was a framework, there were so many abstract things that were not needed. Isn't it a waste. Therefore, it is best to call static functions that have already been abstracted, rather than repeat them again. Here there is a for loop, that is, we need to have each. What are the results? It is hard to implement it. Because we always need to create an array, and finally return the new array, We have to extract a function to call it again. This does not violate the original intention?

After a glance on the internet, I finally stared at prototype. His implementation method is to abstract a function for processing recursive increments and use this function for recursion. What should I do? This is called a framework. The following is a recursive function:

Function inject (memo, iterator, context ){
This. each (function (value, index ){
Memo = iterator. call (context, memo, value, index );
});
Return memo;
}

The final implementation of this flatten function is as follows. The code is really beautiful:

Function flatten (){
Return this. inject ([], function (array, value ){
If (Object. isArray (value ))
Return array. concat (value. flatten ());
Array. push (value );
Return array;
});
}

Of course, another abstract function is needed to process the for loop, which is our each function. Take this each function out of flatten and learn jQuery's practices and add native support. Of course, you can also process pure objects, not just Arrays:

Each: function (callback, bind ){
Var isObject = arale. typeOf (this. obj) = 'object ',
I = 0,
Key;
If (isObject ){
Var obj = this. obj;
For (key in obj ){
If (callback. call (bind, key, obj [key]) === false ){
Break;
}
}
} Else {
Var arr = this. obj;
If (Array. prototype. forEach ){
// When the user returns false, the execution will continue.
// Native? Why? Why? Marked TODO;
Return []. forEach. call (arr, callback, bind );
};
For (var value = arr [0], length = arr. length; I <length & callback. call (bind, I, value )! = False; value = arr [++ I]) {};
}
}

==========

Recently, many Javascript programs have been used. I have taken a look at the latest articles, and the articles posted on the team's internal blog, all of which are Javascript .? Why does the mother seek P? Rubber and aluminum Co., Ltd.

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.