A simple example of Array reordering method and operation method

Source: Internet
Author: User
Tags comparison sort

  The following small series is a simple introduction to reverse (), sort (), concat (), slice (), splice () method in the array. Come and have a look.

The code is as follows: <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <html xmlns=" http://www.w3.org/1999/xhtml "> <head>     <title> reordering method and operation method </title >     <script type= "Text/javascript" >        //Sort method       function Basicsort () {         var values=[0,10,2,3,12,5];          alert (values . reverse ()); the//reverse () method simply turns the order of the array in turn          alert (Values.sort ()), and the//sort () method compares the strings. Most cases are not the best scenario          alert (Values.sort (Compare));//sort () method can receive a comparison function as a parameter      }        //Custom comparison functions, the returned array is ascending, or you can change the code, such as Value1<value2 return 1, to achieve the result is descending.       Function compare (value1,value2) {        if (value1 < value2) {    &N Bsp     return-1;        }eLSE if (Value1 > value2) {         return 1;        }else{    & nbsp     return 0;        }       {       //action method The Concat () method creates a new array based on all the items in the current array &nb Sp     Function Basicconcat () {        var colors=["Red", "Blue", "pink"];       & nbsp var colors2=colors.concat ("Yellow", ["Black", "Brown"]);//red,blue,pink,yellow,black,brown         Alert (COLORS2);      }        /The method is to create a new array based on all the items in the current array and receive one or two parameters, that is, intercept  end > str >=start (That is, items that do not include the end position)       function Basicslice () {         var colors=["Red", "Blue", "pink "," Yello "," White "];          var colors2=colors.slice (1);          var colors3=colors.slice (1,4);          alert (COLORS2);          alert (COLORS3);      }         function Basicsplice () {         var colors=[" Red "," Blue "," Pink "," Yello "," White "];          var removed=colors.splice (0,2)//table deletion is deleted the first two          alert ("delete Items: "+removed+"----Current items: "+colors"          var Inserted=colors.splice (1,0, "Black", "gray"); Indicates deletion of 0 items at location 1, insert new add          alert ("Current item:" +colors);      }       </script> </head> <body>   <input type= "button" Valu E= "sorted sort" onclick= "basicsort ();"/>   <input type= "button" value= "Concat" onclick= "Basicconcat ();"/> & nbsp <input type= "button" value= "Slice" onclick= "basicslice ();"/>   <input type= "button" value= "Splice" Onclick= "Basicsplice ();"/> </body> </html>  

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.