Js Array usage

Source: Internet
Author: User

[Javascript]
<Script type = "text/javascript">
 
Var arr = new Array ();
Arr [0] = "aaa ";
Arr [1] = "bbb ";
Arr [2] = "ccc ";
Alert (arr. length); // 3

Arr. pop ();
Alert (arr. length); // 2
Alert (arr [arr. length-1]); // bbb

Arr. pop ();
Alert (arr [arr. length-1]); // aaa
Alert (arr. length); // 1
 
 
Var arr2 = new Array ();
Alert (arr2.length); // 0

Arr2 [0] = "aaa ";
Arr2 [1] = "bbb ";
Alert (arr2.length); // 2

Arr2.pop ();
Alert (arr2.length); // 1
Alert (arr2 [arr2.length-1]); // aaa

/*
ArrayObj. slice (start, [end])
The slice Method returns an Array object, which contains the specified part of arrayObj.
The slice Method is always copied to the end specified element, but this element is not included. If start is negative, it is processed as length + start. Here, length is the length of the array. If end is negative, it is processed as length + end. Here, length is the length of the array. If end is omitted, the slice method will always be copied to the end of arrayObj. If end appears before start, no elements are copied to the new array.
*/
Arr2 = arr2.slice (0, arr2.length-1 );
Alert (arr2.length); // 0
Arr2 [0] = "aaa ";
Arr2 [1] = "bbb ";
Arr2 [2] = "ccc ";
Arr2 = arr2.slice (0, 1 );
Alert (arr2.length); // 1
Alert (arr2 [0]); // aaa
Alert (arr2 [1]); // undefined
 

// Shift: Delete the first entry of the original array and return the value of the deleted element. If the array is empty, undefined is returned.
Var a = [1, 2, 3, 4, 5];
Var B = a. shift ();
Alert (a + ";" + B); // a: [2, 3, 4, 5] B: 1
 
 
// Unshift: add the parameter to the beginning of the original array and return the length of the array. note: In IE6.0, the test return value is always undefined, and in FF2.0, the test return value is 7. Therefore, the return value of this method is unreliable. You need to use splice instead of this method when returning the value.
Var a = [1, 2, 3, 4, 5];
Var B = a. unshift (-2,-1 );
Alert (a + ";" + B); // a: [-2,-,] B: 7
 
 
// Pop: Delete the last entry of the original array and return the value of the deleted element. If the array is empty, undefined is returned.
Var a = [1, 2, 3, 4, 5];
Var B = a. pop ();
Alert (a + ";" + B); // a: [1, 2, 4] B: 5

 
// Push: add the parameter to the end of the original array and return the length of the array.
Var a = [1, 2, 3, 4, 5];
Var B = a. push (6, 7 );
Alert (a + ";" + B); // a: [1, 2, 3, 4, 5, 6, 7] B: 7

 
// Concat: returns a new array consisting of adding parameters to the original array.
Var a = [1, 2, 3, 4, 5];
Var B = a. concat (6, 7 );
Alert (a + ";" + B); // a: [1, 2, 3, 4, 5] B: [1, 2, 3, 4, 5, 7]
 
 
// Splice (start, deleteCount, val1, val2 ,...): Delete the deleteCount item from the start position, and insert val1, val2 ,..., the returned result is the deletion item array.
// When clearing the array, you only need to pass startIndex; if you do not delete all elements, then pass the deleteCount parameter.
// Splice also has the ability to delete and then add, that is, delete several elements first, and then add several elements at the location of the deletion. The deletion and addition of the number of elements must not be equal, deleteCount is also used at this time.
Var a = [1, 2, 3, 4, 5];
Var B = a. splice (, 9); // Delete 2 from subscript 2 and add three items (, 9) from position 2)
Alert (a + ";" + B); // a: [1, 2, 7, 8, 9, 5] B: [3, 4]

Var B = a. splice (0, 1); // same as shift
Alert (a + ";" + B); // a: [2, 7, 8, 9, 5] B: [1]

 
Var a = [1, 2, 3, 4, 5];
A. splice (0, 0,-2,-1 );
Var B = a. length; // same as unshift
Alert (a + ";" + B); // a: [-2,-,] B: [7]

Var B = a. splice (a. length-1, 1); // same as pop
Alert (a + ";" + B); // a: [-2,-, 4] B: [5]

A. splice (a. length, 0, 6, 7 );
Var B = a. length; // same as push
Alert (a + ";" + B); // a: [-2,-, 7] B: [8]

// Reverse: returns the reverse order of the array.
Var a = [1, 2, 3, 4, 5];
Var B = a. reverse ();
Alert (a + ";" + B); // a: [5, 4, 3, 2, 1] B: [5, 4, 3, 2, 1]
 
// Sort (orderfunction): sorts arrays by specified parameters.
Var a = [1, 4, 3, 2, 5];
Var B = a. sort ();
Alert (a + ";" + B); // a: [1, 2, 3, 4, 5] B: [1, 2, 3, 4, 5]
 
// Slice (start, end): returns a new array consisting of items from the specified start subscript to the end subscript in the original array.
Var a = [1, 2, 3, 4, 5];
Var B = a. slice (2, 5 );
Alert (a + ";" + B); // a: [1, 2, 3, 4, 5] B: [3, 4, 5]
 
// Join (separator): groups the elements of the array into a string that uses the separator. If this parameter is omitted, a comma is used as the separator by default.
Var a = [1, 2, 3, 4, 5];
Var B = a. join ("| ");
Alert (a + ";" + B); // a: [1, 2, 3, 4, 5] B: "1 | 2 | 3 | 4 | 5"
Var b2 = a. join ();
Alert (a + ";" + b2); // a: [1, 2, 3, 4, 5] B: "1, 2, 3, 4, 5"
 
 
// String processing function
Function StringBuffer (){
Var arr = new Array;
This. append = function (str ){
Arr [arr. length] = str;
Return this;
};
 
This. toString = function (){
Return arr. join ("|"); // ping the append array into a string.
};
}
 
Var strBuf = new StringBuffer ();
StrBuf. append ("1"). append ("2"). append ("3"); // strBuf. append (arg)
StrBuf. append ("4 ");
StrBuf. append ("5 ");
Alert (strBuf. append ("6 "). append ("7 "). append ("8 "). toString (); // 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
 
 
// Convert the array into a string separated by a specific symbol
Function arrayToString (arr, separator ){
If (! Separator)
Separator = ""; // If separator is null, It is null by default.
Return arr. join (separator );
}
Var arr = new Array (1, 3, 5 );
Alert (arrayToString (arr, "="); // 1 = 3 = 5
 
// Search for strings contained in the array
Function arrayFindString (arr, string ){
Var str = arr. join ("");
Return str. indexOf (string );
}
Var arr = new Array (1, 3, 5 );
Alert (arrayFindString (arr, 3); // 1

</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.