Operation of the array

Source: Internet
Author: User
Tags array sort array to string

  • Create an array
  • <script>
    var i;
    var mycars = new Array ();
    Mycars[0] = "Saab";
    MYCARS[1] = "Volvo";
    MYCARS[2] = "BMW";
    for (i=0;i<mycars.length;i++) {
    document.write (Mycars[i] + "<br/>");
    }
    </script>
  • Merging two arrays-concat ()
  • <script>
    var hege = ["Cecilie", "Lone"];
    var stale = ["Emil", "Tobias", "Linus"];
    var children = Hege.concat (stale);
    document.write (children);
    </script>
  • Merging three arrays-concat ()
  • <script>
    var parents = ["Jani", "Tove"];
    var = ["Stale", "Kai Jim", "Borge"];
    var children = ["Cecilie", "Lone"];
    var family = Parents.concat (children);
    document.write (family);
    </script>
  • Make a string of elements of an array-join ()
  • <script>
    function MyFunction () {
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    var X=document.getelementbyid ("demo");
    X.innerhtml=fruits.join ();
    }
    </script>
  • Delete the last element of an array-pop ()
  • <script>
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    function MyFunction () {
    Fruits.pop ();
    var X=document.getelementbyid ("demo");
    X.innerhtml=fruits;
    }
    </script>
  • Add a new element to the end of the array-push ()
  • <script>
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    function MyFunction () {
    Fruits.push ("Kiwi")
    var X=document.getelementbyid ("demo");
    X.innerhtml=fruits;
    }
    </script>
  • Reverses the order of elements in an array-reverse ()
  • <script>
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    function MyFunction () {
    Fruits.reverse ();
    var X=document.getelementbyid ("demo");
    X.innerhtml=fruits;
    }
    </script>
  • Delete the first element of an array-shift ()
  • <script>
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    function MyFunction () {
    var Delell = Fruits.shift ();
    var X=document.getelementbyid ("demo");
    X.innerhtml= ' Delete after array is: ' + fruits;
    document.getElementById ("Demo2"). Innerhtml= ' deleted elements are: ' + Delell;
    }
    </script>
  • Select elements from an array-slice ()
  • <script>
    function MyFunction () {
    var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
    var citrus = Fruits.slice (1,3);
    var X=document.getelementbyid ("demo");
    X.innerhtml=citrus;
    }
    </script>
  • Array sort (ascending alphabetical order)-sort ()
  • <script>
    function MyFunction () {
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    Fruits.sort ();
    var X=document.getelementbyid ("demo");
    X.innerhtml=fruits;
    }
    </script>
  • Sort by number (ascending in numeric order)-sort ()
  • <script>
    function MyFunction () {
    var points = [40,100,1,5,25,10];
    Points.sort (function (A, b) {return-A-b});
    var X=document.getelementbyid ("demo");
    x.innerhtml=points;
    }
    </script>
  • Sort by number (descending in numeric order)-sort ()
  • <script>
    function MyFunction () {
    var points = [40,100,1,5,25,10];
    Points.sort (function (A, b) {return b-a});
    var X=document.getelementbyid ("demo");
    x.innerhtml=points;
    }
    </script>
  • Add an element to the 2nd position of the array-splice ()
  • <script>
    function MyFunction () {
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    Fruits.splice (2,0, "Lemon", "Kiwi");
    var X=document.getelementbyid ("demo");
    X.innerhtml=fruits;
    }
    </script>
  • Convert array to string-tostring ()
  • <script>
    function MyFunction () {
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    var str = fruits.tostring ();
    var X=document.getelementbyid ("demo");
    X.innerhtml= str;
    }
    </script>
  • Add a new element at the beginning of the array-unshift ()
  • <script>
    function MyFunction () {
    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    Fruits.unshift ("Lemon", "Pineapple");
    var X=document.getelementbyid ("demo");
    X.innerhtml=fruits;
    }
    </script>

Operation of the 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.