This article mainly introduces the use of Array in JS. For more information, see new Array ()
New Array (len)
New Array ([item0, [item1, [item2,...]
Method to use an array object:
Var objArray = new Array ();
ObjArray. concact ([item1 [, item2 [,...] ------------------- connect the parameter list to the end of objArray to form a new array and return it. The original array is not affected. For example, var arr = ["a", "B", "c"];
Arr. concact ("d", "e ");
Returns an array containing the letter elements from "a" to "e. The arr itself is not affected.
ObjArray. join (separator) ----------------- converts an array into a string using the character specified by separator as a delimiter. When seperator is a comma, it acts the same way as toString.
ObjArray. pop () --------- in general, it is the last element of the pop-up array. The following push method makes it possible to use arrays as stacks. The pop method returns the value of the last element of the array and reduces the length attribute by 1. This means that the last element is lost immediately after the return result.
ObjArray. push ([value1 [, value2 [,...]) ----------- Add the parameter to the end of the array. For example, [1, 2, 3, 4]. push ("a", "B") will get [1, 2, 3, 4, "a", "B"]
ObjArray. reverse () sorts the elements in the array in reverse order. For example, [, 3]. reverse () will get [, 1]. This operation is performed on the original array, and the array itself is also returned.
ObjArray. shift () ----------- removes the first element of the array and returns the value of this element. This method is similar to the pop method. The pop method removes the last element.
ObjArray. slice (start, end) ----------- returns a subset of the array object. The index starts from start (including start) to end (excluding end), and the original array is not affected. For example, [,]. slice () produces [, 4]. When 'start' or 'end' is a negative number, use them to add the 'length' value. For example, [1, 2, 3, 4, 5]. slice (-4,-1) will get [3, 4, 5]. If end is less than or equal to start, an empty array is returned.
ObjArray. sort (comparefn) ------- sorts an array based on the size comparison function defined by comparefn. The comparefn function must accept two parameters: element1 and element2. If you want element1 to be placed before element2, a negative number is returned. If you want element1 to be placed after element2, a positive number is returned, if the two numbers are treated equally (that is, the original order is maintained), 0 is returned. When comparefn is omitted, the elements are arranged alphabetically. For example, for the defined comparison function cmp: function cmp (e1, e2) {return E1-E2;} Then [,]. sort (cmp) will get [,].
ObjArray. splice (start, deleteCount [, item1, item2 [,...]) is a complex function used to delete, replace, and insert array elements. The start parameter indicates the index location for the operation, and the deleteCount parameter indicates the number of elements to be deleted from the start (including the start position). If deleteCount is omitted, the remainder of the array is to be deleted from start. [, Item1 [, item2 [,...] indicates an optional list of elements inserted before start. For example:
Var arr = [0, 1, 2, 3, 4, 5, 6];
Arr. splice (1, 1 );
Document. write (arr); // displays"
Arr = [0, 1, 2, 3, 4, 5, 6];
Arr. splice (0, 0, "a", "B ");
Document. write (arr); // displays "a, B, 6"
Arr = [0, 1, 2, 3, 4, 5, 6];
Arr. splice (3,2, "c", "d ");
Document. write (arr); // display ", 2, c, d, 5, 6"
ObjArray. unshift (item1 [, item2 [,...]) ----------------- Insert the parameter list to the beginning of the array. Its nature and push method type, but the push method adds elements to the end of the array. For example, [1, 2, 3, 4]. unshift ("a", "B") Returns ["a", "B", 1, 2, 3, 4].
The addition and deletion of js array elements have been confusing. Today, I finally found detailed information and gave me the code to test. ^-^
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
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 (); // 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.
Var a = [1, 2, 3, 4, 5];
Var B = a. unshift (-2,-1); // a: [-2,-,] B: 7
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.
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 (); // a: [1, 2, 3, 4] B: 5 // you can directly call it without returning it.
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); // 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); // 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 ,...
When clearing the array, you only need to pass startIndex.
If you do not delete all elements, 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 number of deleted elements is not equal to the number of added elements, deleteCount is also used at this time.
Var a = [1, 2, 3, 4, 5];
Var B = a. splice (, 9); // a: [,] B: []
Var B = a. splice (0, 1); // same as shift
A. splice (0, 0,-2,-1); var B = a. length; // same as unshift
Var B = a. splice (a. length-1, 1); // same as pop
A. splice (a. length, 7); var B = a. length; // same as push
Reverse: returns the reverse order of the array.
Var a = [1, 2, 3, 4, 5];
Var B = a. reverse (); // a: [5, 4, 3, 2, 1] B: [5, 4, 3, 2, 1]
Sort (orderfunction): sorts arrays by specified parameters.
Var a = [1, 2, 3, 4, 5];
Var B = a. sort (); // a: [1, 2, 3, 4, 5] B: [1, 2, 3, 4, 5]
Slice (start, end): returns a new array consisting of items from the original array that specify the start subscript to the end subscript
Var a = [1, 2, 3, 4, 5];
Var B = a. slice (); // a: [, 5] B: [, 5]
Join (separator): A string is set up for the elements of the array. The separator is separator. If it is omitted, a comma is used as the separator by default.
Var a = [1, 2, 3, 4, 5];
Var B = a. join ("|"); // a: [1, 2, 3, 4, 5] B: "1 | 2 | 3 | 4 | 5"
Then we can use an array to simulate javaStringBuffer to process strings:
The Code is as follows:
/**
* String processing functions
*/
Function StringBuffer (){
Var arr = new Array;
This. append = function (str ){
Arr [arr. length] = str;
};
This. toString = function (){
Return arr. join (""); // ping the append array to a string.
};
}
Today, in the application, we suddenly found that join is a good way to convert arrays into strings, so it is encapsulated into objects using:
The Code is as follows:
/**
* Convert an array to a string separated by a specific symbol.
*/
Function arrayToString (arr, separator ){
If (! Separator) separator = ""; // If separator is null, the default value is null.
Return arr. join (separator );
}
/**
* Search for strings contained in an array
*/
Function arrayFindString (arr, string ){
Var str = arr. join ("");
Return str. indexOf (string );
}