The slice () function) and the sort, out-of-order, and search (sort () function) functions of JavaScript Arrays)

Source: Internet
Author: User

All operations on variables in JavaScript are referenced, and arrays are the same.
I tried to copy an array two days ago, but I was unable to find a solution (the Traversal method is definitely not used)

I accidentally picked up the JavaScript authoritative guide and flipped through the array operation functions and found the slice () function.

Slice () was originally used to capture a part of the array. Here I use it to copy the array. Its format is as follows:
Array. slice (start, end)
If the end parameter is omitted, the split array contains all elements starting from start to the end of the array.

Now we need to use it to copy the array, just one line, haha:
Var newArray = oldArray. slice (0 );

All these functions can be completed with an sort.

1. Sorting:
The default sort () is sorted by character encoding:
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Now we want to sort it by numerical value:
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

You only need to pass a comparison function to sort. If the value of the comparison function is smaller than 0, it means that a must appear before B; otherwise, a must follow B.
2. disordered order:
Let the comparison function randomly return-1 or 1:
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

3. Search:
It's quite fresh to use sort () for searching. Let's see how it works.
Now I want to find out all the elements in the array containing the letter 'A'. If there is no sort (), it seems that I can only use traversal. The traversal efficiency... That's horrible !! The procedure is as follows:
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

In this way, all the elements in the array containing the letter 'A' are placed in front of the array. After some simple processing, the search results can be output.
Using slice to copy arrays is indeed a good method, and in fact it will be used more often, but it is hard to say the efficiency is better than the traversal, because no one of us knows how js slice is implemented, but it should be highly efficient. More importantly, it is much simpler to write.

The second sorting method is really clever, so you don't have to write your own shuffling algorithm ^

As for the third... if you do not need regular expressions and must use Sort, it is much simpler to write, but the efficiency may not be higher than that of traversal. Knowing that traversal does not mean low efficiency, the traversal algorithm of one-way search for linear tables is O (N) complex. I guess the Js sort algorithm is q-sort (it would be too cool if it is bubble). ^ ), therefore, at least O (N * LogN) is complex (sorting is easier to understand than searching ), that is to say, in execution efficiency, matching with Sort is basically not as fast as directly traversing the topology.
I used to use concat to copy arrays. For example, newArr = oldArr. concat () has the same efficiency as slice (0.

I tested that copying an array through slice or concat is much faster than traversing.
During the test, I also found another feature. When an array uses a subscript value, it seems that it also uses a method similar to traversal. For example, if the array arr has 1 million array elements, the two expressions: I = arr [0] And I = arr [999999] will get the value faster! (If you do not believe it, You can compile an example by yourself ). That is to say, when the array is traversed, the time required for each value increases with the increase of the lower value.

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.