This article describes how to operate javascript built-in objects. For more information, see
1. indexOf () method----- This method is commonly used.
Returns the position of the first occurrence of a specified string value.
Format: stringObject. indexOf (substring, startpos)
For example, find the seat where the second o is located.
Var mystr = "Hello World! "
Document. write (mystr. indexOf ("o", mystr. indexOf ("o") + 1); -------- result: 7
Mystr. indexOf ("o") ----- the result is 4, which is found from the following table of the string.
Document. write (mystr. indexOf ("o", 5); start from the fifth position.
Result: 7
Note: The. indexOf () method is case sensitive.
. If the string value to be retrieved does not appear, this method returns-1. ·
2. split string ()
The split () method splits the string into a string array and returns this array.
Syntax:
StringObject. split (separator, limit)
Note: If you use an empty string ("") as a separator, each character in the stringObject will be separated.
For example:
Var mystr = "86-010-85468578 ";
Separator String object mystr with the character "-"
Document. write (mystr. split ("-") +"
"); --- Result 86,010,854 68578
Separate each character of the string object mystr
Document. write (mystr. split ("") +"
"); --- Result:,-, 0 ,-,
Splits String object mystr into characters and splits it three times
Mystr. split ("", 3) ------------ result 8, 6 ,-
3. Extract the string substring ()
The substring () method is used to extract characters of a string between two specified subscripts.
Syntax:
StringObject. substring (starPos, stopPos)
Note:
1. The returned content is all characters starting from start (including the characters at the start position) to stop-1. Its length is stop minus start.
2. If the start parameter is the same as the stop parameter, this method returns an empty string (a string with a length of 0 ).
3. If the start parameter is greater than the stop parameter, this method swaps the two parameters before extracting the substring.
For example:
Var mystr = "Hello World! "
Document. write (mystr. substring (6) +"
"); ---- Intercept all the characters starting with subscript 6 --- the result is World!
Document. write (mystr. substring (5, 4 ));
Document. write (mystr. substring (); ------ the result is that the number of digits obtained by o is 5-4.
4. Extract the specified number of characters substr ()
The substr () method extracts a specified number of strings starting from the startPos position from the string.
Syntax:
stringObject.substr(startPos,length)
Note: If the startPos parameter is a negative value, the position starts from the end of the string. That is to say,-1 refers to the last character in the string,-2 refers to the second to last character, and so on.
If startPos is negative and the absolute value is greater than the string length, startPos is 0
Example: var mystr = "Hello World! ";
Document. write (mystr. substr (6) +"
"); ----------- Intercept World
Document. write (mystr. substr (0, 5); ---- intercept ----- Hello
5. Math
Round ()
Math. round (x)
The random () method returns a value ranging from 0 ~ A random number between 1 (greater than or equal to 0 but less than 1.
Math.random();
6. Method of Array object
The concat () method is used to connect two or more arrays. This method returns a new array without changing the original array.
Syntax
ArrayObject. concat (array1, array2,..., arrayN)
Note: This method does not change the existing array, but only returns a copy of the connected array.
Var myarr1 = new Array ("010 ")
Var myarr2 = new Array ("-", "84697581 ");
Var mycon = myarr1.concat (myarr2 );
Document. write (mycon) --- output 010,-, 84697581
The description is just a copy.
The join () method is used to put all elements in the array into a string. Elements are separated by the specified delimiter.
Var myarr1 = new Array ("86", "010 ")
Var myarr2 = new Array ("84697581 ");
Var myarr3 = myarr1.concat (myarr2 );
Document. write (myarr3.join ("") +"
"); ------ Result 8601084697581
Document. write (myarr3.join ("_") +"
"); ----- The result is 86_010_84697581.
Document. write (myarr3.join () +"
"); ----- The result is 86,010,846 97581. The default value is, and the number is separated.
7. reverse the array element order reverse ()
The reverse () method is used to reverse the order of elements in the array.
Var myarr1 = ["I", "love", "you"];
Document. write (myarr1.reverse (); ----- the result is you, love. Please do not write a reserve error.
8. Select the slice () Element ()
The slice () method returns the selected element from an existing array.
arrayObject.slice(start,end)
1. Return a new array containing elements in the arrayObject from start to end (excluding this element.
2. This method does not modify the array, but returns a child array.
Var myarr1 = ["I", "love", "you"];
Document. write (myarr1.slice () ---- the result is -- love. You are the same as substring, but this is for the array. In fact, the number is 3-1 = 2.
9. array sorting sort ()
The sort () method sorts the elements in the array in a certain order.
Syntax:
ArrayObject. sort (method function)
1. If you do not specify <方法函数> In the order of unicode codes.
2. If you specify <方法函数> , Then press <方法函数> Sort by the specified sorting method.
Function sortNum (a, B ){
Return a-B;
}
Var myarr = new Array ("80", "16", "50", "6", "100", "1 ");
Document. write (myarr. sort (sortNum); ---- j: 80,100
If it is B-a, the result is, 80, 50, 16, 6, 1.
The above is my personal summary of some common javascript built-in object operations, I hope you will like it.