JavaScript Array Flatten and recursive use introduction _javascript tips

Source: Internet
Author: User
Tags javascript array
How do you use JavaScript to [1,2,3,[4,5, [6,7]], [[[8]]] so that an Array becomes [1,2,3,4,5, 6,7,8]? The legendary Array flatten.

To handle this problem, we usually need recursion to allow the program to loop itself according to an algorithm. In a book that says, "recursion is a powerful programming technique," Well, she's not just a JavaScript. Recursion can be difficult, and it can be simpler (in some case it's still more difficult). Deal with the above problem, with the recursive return solution, should be more appropriate. Before the coworker realized this, it is a simple recursive use of the example bar:
Copy Code code as follows:

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;
}

In the IF (Group[i] instanceof Array), the function itself is called, recursive by passing parameters. Just in reconstructing the array.js, I feel that since it is a framework, so much abstraction out of the things do not, is not too wasteful. So it's best to call a static function that has been abstracted instead of repeating it again. Here's A For loop, which means we'll need to have each. What's the result? Four words, not a good implementation. Because we always have to create an array, and finally return the new array, we have to pull out a function to tune it. This is not against the original intention?

A glance at the net, and finally stared at the prototype. His approach is to abstract a function that deals with recursive increments and then use this function to do recursion. What do you say? To say, this is called framework. The following is a function that handles recursion:
Copy Code code as follows:

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

And this flatten function, the final implementation is this, the code is really beautiful:
Copy Code code as follows:

function Flatten () {
return This.inject ([], function (array, value) {
if (Object.isarray (value))
Return Array.concat (Value.flatten ());
Array.push (value);
return array;
});
}

Of course, we need another abstract function to handle the for loop, which is our each function. By the way in flatten, take out this each function, learn the jQuery approach, add native support, and of course, you can also handle pure objects, not just arrays:
Copy Code code as follows:

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) {

User return false; will continue to execute.
The original is very embarrassing, go or give up? 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 = AR R[++i]) {};
}
}

I've been playing Javascript more recently. A glance at the recent article, as well as in the team internal blog posted on the article, are all JS. 囧 Seems to be a big change. Need a balance.

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.