The array and its operations under javascript [from http://blog.zjol.com.cn/173915/spacelist-blog]

Source: Internet
Author: User
This article introduces the following aspects:

1. How to create an array
2. How to operate the Array (add, delete, read)
3. Common array methods and attributes

How to create an array is generally divided into three types based on Initialization settings:

1. Create an array:
Var arr = new Array ();
Key Point: use the new keyword to create an Array object Array (). The Array () object is a local class. You can use new to create an object and then use
2. Specify the array size when creating an array:
Var arr = new Array (10); // an Array with an initial size of 10 is created.
Note: When the array size is used for initialization, the array will be automatically extended without errors like in C. dynamic Growth is a property of js arrays. in addition, js supports a maximum length of 4294967295
3. Direct initialization:
Var arr = new Array ("crawler", "love", "Mao"); // The Array is initialized directly here.
Or var arr = ["crawler", "love", "Mao"]; // brackets can also declare an array object

Of course, similar to the C language, you can also define 2-dimensional, 3-dimensional, and multi-dimensional arrays, which are not discussed here.

Attribute of the array: length
Arr. length returns the length of the array arr, which is common in traversing arrays in a loop. For example:
For (var I = 0; I <arr. length; I ++ ){
Execution part
}

Access to array elements: arr [index], where index indicates the base number of the array. It starts from 0 and has a total of arr. length element. for example, if arr [0] accesses the first array element, arr [1] accesses the second array element .... operation Methods of arrays by analogy: first, we will give an overview of the common methods of these operation arrays (13)

ToString (), valueOf (), toLocalString (), join (), split (), slice (), concat (),
Pop (), push (), shift (), unshift (), sort (), splice ()

The functions and usage of these methods are analyzed one by one.

ToString (), valueOf (), toLocalString ():
Function: returns all elements of an array.
Note: The array name can also return the entire array.

Code:

  1. Var m = ["am", "bm", "cm"]; // declare an array object with parentheses
  2. Alert (m. toString (); // toString () returns all content of the array object, separated by commas (,), that is, am, bm, cm
  3. Alert (m. valueOf (); // valueOf () also returns all contents of the array object
  4. Alert (m. toLocaleString (); // toLocaleString () also returns all contents of the array object, but there are differences in regional languages.
  5. Alert (m); // array name also returns all contents of the array object
Var m = ["am", "bm", "cm"]; // declare an array object alert (m. toString (); // toString () returns all contents of the array object, separated by commas (,), that is, am, bm, cm alert (m. valueOf (); // valueOf () also returns all contents of the array object alert (m. toLocaleString (); // toLocaleString () also returns all the content of the array object, but alert (m) is not studied for the time being due to regional language differences ); // The array name also returns all contents of the array object.

Run: <script> var m = ["am", "bm", "cm"]; alert (m. toString (); alert (m. valueOf (); alert (m. toLocaleString (); </script>

Join ():
Function: concatenates all items in the array with a certain character (string), but does not modify the original array

Code:

  1. Var m = ["am", "bm", "cm"]; // declare an array object with parentheses
  2. Var n = m. join ("---"); // use --- to connect am, bm, cm.
  3. Alert (m. toString (); // m is not modified, and am, bm, cm are returned.
  4. Alert (n); // n is a string, which is am --- bm --- cm
Var m = ["am", "bm", "cm"]; // declare an array object var n = m. join ("---"); // use --- to connect am, bm, cm. alert (m. toString (); // m is not modified. The returned am, bm, cm alert (n); // n is a string, which is am --- bm --- cm.

Run: <script> var m = ["am", "bm", "cm"]; var n = m. join ("---"); alert (m. toString (); alert (n); </script>

Split ():
Function: Splits a string into an array based on a specific character (string) without modifying the original string.

Code:

  1. Var str = "I love maomao, I am caolvchong ";
  2. Var arr = str. split ("o"); // split the str string into an array by character o
  3. Alert (arr); // outputs the entire array
Var str = "I love maomao, I am caolvchong"; var arr = str. split ("o"); // split the str string into an array alert (arr) by character o; // output the entire array

Run: <script> var str = "I love maomao, I am caolvchong"; var arr = str. split ("o"); alert (arr); </script>

Slice (): returns the array part of a starting position (ending at a position) without modifying the original array.

Code:

  1. Var m = ["am", "bm", "cm", "dm", "em", "fm"];
  2. Var n = m. slice (2); // return the elements following the second element bm, that is, cm, dm, em, fm
  3. Var q = m. slice (); // returns the second element and the fifth element, that is, cm, dm, em.
  4. Alert (n );
  5. Alert (q );
Var m = ["am", "bm", "cm", "dm", "em", "fm"]; var n = m. slice (2); // returns the elements after the second element bm, that is, cm, dm, em, fm var q = m. slice (); // returns the second to the fifth element, that is, cm, dm, em alert (n); alert (q );

Run: <script> var m = ["am", "bm", "cm", "dm", "em", "fm"]; var n = m. slice (2); var q = m. slice (2, 5); alert (n); alert (q); </script>

Stack operations for array objects:
Push (): Add an item at the end of the array.
Pop (): deletes the last entry of the array.

Code:

  1. Var m = ["am", "bm", "cm", "dm", "em", "fm"];
  2. M. push ("gm"); // Add the element gm to the end of the array
  3. Alert (m );
  4. M. pop (); // Delete the last element of the array gm
  5. Alert (m );
Var m = ["am", "bm", "cm", "dm", "em", "fm"]; m. push ("gm"); // Add the element gm alert (m); m. pop (); // Delete the last element of the array gm alert (m );

Run: <script> var m = ["am", "bm", "cm", "dm", "em", "fm"]; m. push ("gm"); alert (m); m. pop (); alert (m); </script>

Queue operations for array objects:
Unshift (): adds an entry to the array header.
Shift (): deletes the first entry of the array.

Code:

  1. Var m = ["am", "bm", "cm", "dm", "em", "fm"];
  2. M. unshift ("gm"); // Add the element gm to the first element of the array
  3. Alert (m );
  4. M. shift (); // Delete the first element of the array gm
  5. Alert (m );
Var m = ["am", "bm", "cm", "dm", "em", "fm"]; m. unshift ("gm"); // Add the element gm alert (m) to the first element of the array; m. shift (); // Delete the first element of the array gm alert (m );

Run: <script> var m = ["am", "bm", "cm", "dm", "em", "fm"]; m. unshift ("gm"); alert (m); m. shift (); alert (m); </script>

Sort (): sorts arrays by ASCII code and modifies array objects.
Note: Even numeric arrays are converted to strings for comparative sorting.

Code:

  1. Var m = ["am", "fm", "gm", "bm", "em", "dm"];
  2. M. sort (); // sort by letter
  3. Alert (m );
Var m = ["am", "fm", "gm", "bm", "em", "dm"]; m. sort (); // sort alert (m) in alphabetical order );

Run: <script> var m = ["am", "fm", "gm", "bm", "em", "dm"]; m. sort (); alert (m); </script>

Concat (): add elements at the end of the array without modifying the array object.

Code:

  1. Var m = ["am", "bm"]
  2. Var n = m. concat ("cm"); // Add a cm entry and assign the new array object
  3. Alert (m); // The original array is not modified
  4. Alert (n); // outputs a new array object
Var m = ["am", "bm"] var n = m. concat ("cm"); // Add a cm and assign the new array object alert (m); // The original array is not modified alert (n ); // output new array object

Run: <script> var m = ["am", "bm"]; var n = m. concat ("cm"); alert (m); alert (n); </script>

Splice (): add, delete, or replace elements in any position of the array, and directly modify the array object.
Details:
Splice () has three or more parameters. The first two are required, and the following parameters are optional.
Add: splice (start item, 0, add item)
Delete: splice (start item, number of items to be deleted)
Replace: splice (Starting item, number of replicas, and replacement items) is actually the common result of adding and deleting

Code:

  1. Var m = ["am", "bm"]
  2. M. splice (, "fm", "sm"); // Add fm and sm to the end of the first item, and return am, fm, sm, bm
  3. Alert (m );
  4. M. splice (); // Delete the next item of the second item (I .e., sm of the third item, return am, fm, bm)
  5. Alert (m );
  6. M. splice (, "mm"); // Replace the next item (that is, the third item, returns am, fm, mm)
  7. Alert (m );
Var m = ["am", "bm"] m. splice (, "fm", "sm"); // Add fm and sm to the end of the first item, and return am, fm, sm, bm alert (m); m. splice (); // Delete the next item (I .e., the third sm, returns am, fm, bm) alert (m); m. splice (, "mm"); // Replace the next item (that is, the third item, returns am, fm, mm) alert (m );

Run: <script> var m = ["am", "bm"]; m. splice (1, 0, "fm", "sm"); alert (m); m. splice (2, 1); alert (m); m. splice (2, 0, "mm"); alert (m); </script>

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.