Several important methods of the 2016-06-06 array

Source: Internet
Author: User
Tags array length

1.

var arr=[1,2,3]arr.push (' abc '); // returns the length of the array is 4 arr=[1,2,3, ' abc '] if it is Arr.unshift (' abc '), and the returned array length is 4,IE6 IE7 does not support return is undefined,arr=[' abc ', ]var arr=[1,2,3];arr.pop (); Delete the last arr=[1,2], Arr.pop () returns the deleted element Arr.shift (); Delete the first Arr=[2,3] Swap location var arr =[' A ', ' B ', ' C ', ' d ']; requirements: A b C moves backward one, d on the top of the resolution: First Take D away Arr.pop (); // The return is D, then place to the first bit  arr.unshift (Arr.pop ()); Request: b c D move forward a bit to the last solution: take a away namely: Arr.shift (), put a in the last Arr.push () Arr.push ( Arr.shift ());

<script>
    //Splice method for hanging and frying days remove replace add function    varArr =[' A ', ' B ', ' C ', ' d ', ' e ']; //DeleteAlert (Arr.splice (0,2));//returned is the deletion of something, starting from the No. 0 bit to delete two bits namely a BAlert (arr);//arr=[c,d,e    //ReplaceAlert (Arr.splice (0,1, ' FFF '));//replacing the No. 0 bit of the array is a, replacing only one bit, so eject aAlert (arr);//fff,b,c,d,eAlert ((Arr.splice (0,2, ' FFF ', ' GGG '))//A B starts with the 0th position, replaces the two bit, and if GGG is not written, the array length is reduced by one bitAlert (arr)//fff,ggg,c,d,e    //AddAlert (Arr.splice (1,0, ' m ', ' n ', ' l '));//add three numbers from where the array is labeled 1, and 0 for addAlert (arr)//a M n l b c d e    //Go heavy    vararr=[1,1,2,2,2,4,4,4,6,4,5,8,8]; Alert (Removerepeat (arr));//1 2 4 6 5 8    //method of extracting the weight    functionRemoverepeat (arr) {if(arr==NULL|| arr==undefined)return;  for(vari=0;i<arr.length;i++){             for(varj=i+1; j<arr.length;j++){                if(arr[i]==Arr[j]) {Arr.splice (J,1);//Note that if you delete the small Mark J, the array length will be reduced by 1j--;//so the elements of arr[j] also need to be reduced by one                }            }        }        returnarr; }    //sort () sorting    //1. The default sort () order is a string comparison, only the first digit is compared    varNums =[1,4,7,8,5,9]; Alert (Nums.sort ());//1 4 5 7 8 9    varChars=[' A ', ' Z ', ' C ', ' f ', ' t ']//a C f t z because a-Z 97-122alert (Chars.sort ()); varNUMS2=[1,4,22,3,8,9,77,56,48];//-->var nums2=[' 1 ', ' 4 ', ' 22 ', ' 3 ', ' 8 ', ' 9 ', ' 77 ', ' 56 ', ' 48 '] ' 77 ' first bit is 7, less than 8 and 9Alert (Nums2.sort ());//1 22 3 4 48 56 77 8 9 Why is this??? because the default sort () sort is a string comparison and only compares the first bit    //How to change this situation, add comparatorAlert (Nums2.sort (function(A, B) {returnA-B;}));//1 3 4 8 9    //How to sort strings is not exactly a number ?    varArr =[' 100px ', ' 20px ', ' 8px ', ' 10px ', ' 200px ']; //alert (Arr.sort ())//default is 100px 10px 200px 20px 8px    //What do you do? Remove Ox and add comparatorAlert (Arr.sort (function(A, B) {returnparseint (a)-parseint (b)));//8px 10px 20px 100px 200px</script>

3. Random number

    Random number    //0-x    math.round (Math.random () *x);    1-x    Math.ceil (Math.random () *x);    Random numbers from X to Y  such as: 100-450    Math.Round (Math.random () * (y-x) +x);

4. Flipping a string

The array is converted into a string with the join ()    var str= ' ABCDEFG ';    var arr=str.split (');    Alert (Arr.reverse (). Join ('));

  

  

Several important methods of the 2016-06-06 array

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.