Randomly sort an array

Source: Internet
Author: User
Tags arrays shuffle
Random Sort

Array.prototype.shuffle = function () {
    var input = this;

    for (var i = input.length-1; I >=0; i--) {

        var randomindex = Math.floor (Math.random () * (i+1)); 
        var itematindex = Input[randomindex]; 

        Input[randomindex] = Input[i]; 
        Input[i] = Itematindex;
    }
    return input;
}

var temparray = [1, 2, 3, 4, 5, 6, 7, 8, 9, ten]
temparray.shuffle ();

And the result is ...

Adding array items

Add one or more array items to the end of the arrays:

var arr = [];
Arr.push (' A ', ' B ');
Console.log (arr); [' A ', ' B ']
Add one or more array items to the front of the arrays:
var arr = [' A ', ' B '];
(Arr.unshift);
Console.log (arr); [1, 2, "a", "B"]
Use the Splice () method to add array entries to the array:
var arr = [' A ', ' B ', ' C ', up];
Arr.splice (2,0, ' d ', ' C ', ' e ');//AT index 2, 0 means no deletion, insert D, C, E
console.log (arr);//["A", "B", "D", "C", "E", "C", 1, 2]
Use the Concat () method to add array items to an array, except that the original array is not changed by using this method:

var arr = [' A ', ' B ', ' C '];
var arr2 = arr.concat (' d ', 1,2,[' E ', 3]);
Console.log (arr); ["A", "B", "C"]
Console.log (ARR2);//["A", "B", "C", "D", 1, 2, "E", 3]

deleting An array item deletes an array entry from the end of the array:

var arr = [' A ', ' B ', ' C ', ' d ', up];
Arr.pop ();
Console.log (arr); ["A", "B", "C", "D", 1]
Delete the first item of the array:
var arr = [' A ', ' B ', ' C ', ' d ', up];
Arr.shift ();
Console.log (arr); ["B", "C", "D", 1, 2]
To delete multiple array entries in an array, except that slice () does not affect the original array, but creates a copy of the array based on the original array:
var arr = [1,2,3,4, ' A ', ' B '];var arr2 = Arr.slice (2);
Console.log (arr); [1, 2, 3, 4, "A", "B"]
Console.log (ARR2);//[3, 4, "A", "B"]console.log (ARR3);//["A", "B"]
The splice () method, in addition to adding array items to an array, can also remove array items from the array, altering the original array:
var arr = [1,2,3,4, ' A ', ' B ', ' C '];
var arr2 = Arr.splice (2,2); At index 2, start deletion of 2
console.log (arr);//[1, 2, "a", "B", "C"]
Console.log (ARR2);//[3, 4]
Query extraction for arrays:

var arr = [1,2,3,4,5,6];
var arr2 = Arr.slice ( -3);
Console.log (arr); [1, 2, 3, 4, 5, 6]
console.log (ARR2);//[4, 5, 6]
Copying an arrayError implementation:
var array1 = new Array ("1", "2", "3"); 
var array2; 
Array2 = array1; 
array1.length = 0; 
alert (array2); Return to Empty
This is wrong because JavaScript is divided into primitive types and reference types (similar to Java, C #). Array is a reference type. Array2 is quoted, so changes to array1 will affect Array2.
Use slice () to copy using slice (), because Slice () is returned as an array.
var array1 = new Array ("1", "2", "3"); 
var array2; 
Array2 = Array1.slice (0); 
array1.length = 0; 
alert (array2); Returns 1, 2, 3  

Using Concat ()
Note that concat () returns a new array instead of the array that called the function, so this can be used for replication.
var array1 = new Array ("1", "2", "3"); 
var array2; 
Array2 = Array1.concat (); 
array1.length = 0; 
SummaryHere is a simple collation of an array of the increment, delete, change, check the relevant methods. A simple summary:
Add Array entry methods: In addition to changing the value of an array item directly and modifying the length of the array to add an array entry method to an array, you can also use push (), Unshift (), concat (), and Splice ().
Delete Array Entry method: Delete Array Item method has pop (), Shift (), slice (), and Splice () method
Change Array Item Method: Change array item mainly by splice () method in array

Query Array Item method: Query Array Item method is actually the log group to do the query extraction function, the main use method is the slice () method

Copy array Note that array is a reference type

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.