& Lt; 1 & gt; Exploring the charm of js special effects array 06

Source: Internet
Author: User

Explore js special effects 06

<! 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>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> js special effect </title>
<! -- <Link id = "link1" rel = "stylesheet" type = "text/css" href = "css1.css"/> -->
<Script>
Function AddNum (){
// Alert (arguments. length );
For (var I = 0; I <arguments. length; I ++ ){
Result + = arguments [I];
}
Alert (result );
}
AddNum (45,445,445,445, 4 );
</Script>
</Head>
<Body>

</Body>
</Html>

 

The following describes how to add and delete elements to an array: add (push) after an array, add (unshift) before an array, and delete (pop) after an array), delete (shift) before the array, as follows:

<Html xmlns = "<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> js special effect </title>
<! -- <Link id = "link1" rel = "stylesheet" type = "text/css" href = "css1.css"/> -->
Var arr = [1, 2, 3];
Alert ("original array content:" + arr );
Arr. push (4); // Add at the end
Alert ("add an element to the end of the array:" + arr );
Arr. unshift (0); // Add a header
Alert ("add an element to the array header:" + arr );
Arr. pop (); // tail deletion
Alert ("after deleting an element to the end:" + arr );
Arr. shift (); // Delete the header
Alert ("delete an element from the array header:" + arr );
</Script>
</Head>
<Body>

</Html>

Alternatively, We can insert, delete, and change data anywhere in the array. We can use splice 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>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> js special effect </title>
<! -- <Link id = "link1" rel = "stylesheet" type = "text/css" href = "css1.css"/> -->
Var arr = [1, 2, 4, 5, 6];
Alert ("prime group:" + arr );
// Delete: splice (starting point, length );
Arr. splice (2, 1 );
Alert ("after deletion 3:" + arr );
 
// Insert: splice (starting point, length, element ...);
Arr. splice (2, 0, 'A', 'B', 'C ');
Alert ("insert element:" + arr );
</Script>
</Head>
<Body>

</Body>
</Html>

 

 

Use join to split the array and add character join:

Var arr = [1, 2, 4, 5, 6];
Var a = arr. join ("--");
Alert ();
</Script>

 

Var arr = ["f", "z", "a", "e", "h", "m"];
Arr. sort ();
Alert (arr );
</Script>

 

<Script>
Var arr = [6,112, 38,454,];
Arr. sort (function (n1, n2 ){
Return n1-n2;
});
Alert (arr );
</Script>

 

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.