JavaScript First (10)----------Array

Source: Internet
Author: User

Definition of array array

   1. Array literals (direct volume)

var arr = [1, 2, 3];

    2. Using constructors

New Array (Length/content)

var arr = new Array (1,2,3,4); arr = [1,2,3,4]

If only one argument is passed, the length of the array is represented. Note: The array can only overflow writes and cannot overflow the read.

Array method (emphasis)7 Ways to change the original array1. Arr.push ()

     Adds an element at the end of the array.

var arr = [1,2,3,4];arr.push (123,[],{})

  

   Write a push method

    

Array.prototype.push = function (target) {    var len = this.length;    (This[len] = target;//one parameter)    for (var i = 0; i < arguments.length; i++) {        This[this.length] = arguments[i];//multiple arguments}

  

2. Arr.pop () do not pass parameters

    Cuts the last element of the array and returns.

3. Arr.shift () do not pass parameters

    Cut the element in front and return.

4. Arr.unshift ()

   Adds an element before the array. Can be one, or multiple, for example

          var arr = [1,2,3,4];          Arr.unshift (0);//******arr = [0,1,2,3,4]

  

          var arr = [1,2,3,4];          Arr.unshift ( -2,-1,0);//************arr=[-2,-1,0,1,2,3,4]

5. Arr.reverse ()

   Reverses the array element.

6. Arr.splice ()

  You can pass in multiple parameters, return a cut array, the first parameter is the start operation bit, the second is the cut length, and the subsequent parameters are the elements added from the operation bit.

var arr = [1,2,3,4];arr.splice (0,2);  Returns an array of cuts [arr=[3,4].

  

when the cut length is 0, you can use it to add elements. This method is commonly used to insert data

var arr = [1,2,3,4];arr.splice (0,0,5,6,7); The return is []        , this time arr= [5,6,7,1,2,3,4]

  

7. Arr.sort () to sort

      Arr.sort () 's own method is to sort by ASCII. Generally we are custom-defined methods, custom rules. Pay attention to the return value, if the return is positive, then the next number is in front, if the return is negative, then the previous number

In front. operand, any two digits in the array are passed to the function, as a, and B, and then sorted according to the return value. For example:

var arr = [2,9,6,1,7,8,3];arr.sort (b) {if (a > B) {return 1;} else{return-1;}} arr [1,2,3,6,7,8,9] positive sequence arr.sort (function (b) {    if (a < b) {        return 1;} else{return-1;}} arr [9,8,7,6,3,2,1]; Reverse

  

Abbreviated as follows:

var arr = [1,6,2,7,8]arr.sort (A, b) {return a-b;//positive order return b-a;//reverse})            

  

Arrange the array of the positive order randomly:

var arr = [1,2,3,4,5,6,7,8];arr.sort (function () {var ret = Math.random ()-0.5;return ret;})

  

To arrange an object according to a property:

var arr = [{name: ' Daming ', age:20},{name: ' Xiaoming ', age:18}]arr.sort (function (A, b) {return a.age-b.age;}

  

all 7 methods can change the original array push () pop () Shift () Unshift () reverse () splice () sort ()   2 ways not to change the original array1. Arr.concat ()

    Connect to two arrays

var arr = [1,2,3];var arr1 = [2,3,4];var arr2 = Arr.concat (arr1); [1,2,3,2,3,4]

  

2. Arr.join ()

    The array is concatenated according to the symbols passed in, and finally converted to a string return. The default is a comma connection.

    

var arr = [1,2,3];arr.join ("-")//"the"

  

3. Arr.tostring ()

    Converts each bit of an array to a string and returns

var arr1= [[],1,2,3];arr1.tostring (); var arr2 = [{},1,2,3];arr2.tostring ();

The results are as follows:

    

4. Str.split () This is the method of the string, manipulating the string, just as opposed to join

      Splits a string into an array of strings in the form of a passed-in symbol

var arr = [1,2,3];var str1 = Arr.join ("-")//"Str.split" ('-');   ["1", "2", "3"]

from the above we can know that this method can change the elements inside the array into the form of a string.

Next See the example consolidation: The fastest way to stitch the following string into a string: "Shanghai", "Beijing", "Zhongshan", "Shenzhen", "Guangzhou", "Meizhou"

Method One: Stack operation (consumption performance)

            

var str = ""; str + + "Shanghai" + "Beijing" + "Zhongshan" + "Shenzhen" + "Guangzhou" + "Meizhou";//"Shanghai Beijing Zhongshan Shenzhen Guangzhou Meizhou"

    Method Two: Heap operations (using arrays)

var arr = ["Shanghai", "Beijing", "Zhongshan", "Shenzhen", "Guangzhou", "Meizhou"];                    Arr.join (");      "Shanghai, Beijing, Zhongshan, Shenzhen, Guangzhou Meizhou"

  

JavaScript First (10)----------Array

Related Article

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.