The operations on variables in JavaScript array quick clone (slice () function) and array sorting, out-of-order and search (sort () function) JavaScript are all referenced, the same applies to arrays.
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:
<Script type = "text/javascript"> <! -- Var testArray = [3,324,534, 665,]; testArray. sort (); alert (testArray); // --> script
[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:
<Script type = "text/javascript"> <! -- Var testArray = [3,324,534, 665,]; testArray. sort (function (a, B) {return a-B;}); alert (testArray); // --> script
[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:
<Script type = "text/javascript"> <! -- Var testArray = [0.5,]; testArray. sort (function () {return Math. random ()>? -1:1;}); alert (testArray); // --> script
[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:
<Script type = "text/javascript"> <! -- Var testArray = ['df ', 'rtr', 'wy', 'daf', 'dfs', 'wefa ', 'tyr', 'rty ', 'rty', 'ryt', 'afds ', 'huer', 'te']; testArray. sort (function (a, B) {return. indexOf ('A') =-1? 1:-1;}); alert (testArray); // --> script
[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.