Better implementation of the JS array connection, the use of knowledge apply.

Source: Internet
Author: User

One of the most recently done Wanda's energy management platform used data connections, and didn't want to write a a.concat (b) directly. Today in the Nuggets saw an optimized scheme. This is the case: A.push.apply (A, b), where a, B is two arrays respectively. Think carefully before you know, the original Concat method will create a new array, and loop A, B, two arrays, and then return the new array. This will not change the value of the B array. But the same creation of a new array also has some performance problems, even small, small. But as a code-neat person, he always wants to optimize his own limits.

Then, the point is up. Let's get to the apply. This is the weight of the most serious, to give people to fish as well as to teach people to fishing. Only by learning the foundation can extrapolate. Learn to apply, the main reference is JS MDN. Self think this is good, there are examples, there is a compatibility explanation, there are parameter description. The most important is the Chinese, very convenient, is there?

General research questions I like to start with three aspects, what,why,how, so get started!

What: succinctly say change the current method's this object and pass in an array parameter, and let this method execute. Let's take a look at the above example to illustrate it. A.push.apply (A, B); This statement shows that before the push method executes, we change the this of the push method to a and pass in the B parameter. Note that the B parameter is an array. Apply takes the values in B and takes them out as arguments to the push method. So the equivalent

Array A is push each item in array B. So the value of the last array of a is the combined value. This will change the value of array A. If there is a need, you can save the array a with a different variable before executing this.

Why: A lot of use, do not explain, just a simple list of MDN, the function of the following code is to find the maximum minimum value in the array.

/* Min/max number in an array */var numbers=[5,6,2,3,7];/* Using Math.min/math.max Apply */var max= Math. Max.Apply(Null, numbers);/* This is about equal to Math.max (Numbers[0], ...) or Math.max (5, 6,..) */var min= Math. Min.Apply(Null, numbers);/* vs. simple loop based algorithm */max=-Infinity, min=+Infinity;For(var i=0; I< numbers. length; I++){if  (Numbers[i > max) max = numbers[i]; if  (Numbers[i < min) min = numbers[i];  
so much to say: how? It should be easy to use! In the end, the use of call and apply is basically the same, the only difference is that the parameter is not an array, but a parameter, equivalent to the array of an item is listed.

Better implementation of the JS array connection, the use of knowledge apply.

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.