Array Introduction
Concept: A set of data that is placed together in a certain order, collectively called an "array". An array is a set of ordered rows of data.
Definition form:
varARR1 =NewArray (1,5,8,7,2,Ten);//defines an array with 6 of data varARR2 =NewArray ();//It simply defines an array (name), but does not give the value (data), which is now empty varARR3 = [1,5,8,7,2,Ten];//with arr1, it is just a shorthand definition method. varARR4 = [];//Same as ARR2, also an empty array.
Use of arrays: the so-called use actually refers to the use of each item of an array.
Value:
var V1 = arr1[0]; // gets the first item in the array arr1, 0 is called subscript var V2 = arr3[3; // gets the 4th item in the array ARR3, 4 is called subscript -the so-called subscript, in fact, is the array of each data "sequence number"-numbering starting from 0, is a continuous integer.
Assignment value:
Arr1[0] = 10; Assigning a value of 10 to the first item of the array arr1 is, in fact, the equivalent of modifying its value, or it can be said to reassign a variable.
arr2[0] ; arr2[1] 33.3; arr2[2] = "444"; arr2[3] = "abc"; arr2[4] true;
At this point the ARR2 array is equivalent to this: [33.3, "444", "abc", True]
The "visual image" of the array (take ARR3 as an example):
Subscript value: 0 1 2 3 4 5 data value: 1 5 8 7 2 Ten
Get the length of an array--that is, the syntax for the number of data in it:
var v1 = array name. length;
Special Note: The maximum subscript for an array is the length of the array minus 1.
The usual pattern of array traversal:
var len = array name. length;
for (var i = 0; i < Len; i++)
{
Here is the processing of each item of an array, each of which is written as: the name [i]
}
Another form of array traversal is the--for in loop statement.
for (Var v1 in array name arr1)
{
Here is the loop body, which is a traversal loop specifically for the array arr1, where the value of V1 is the subscript value representing each item of the array.
V1 is just a "temporary variable" that represents each subscript, which in turn changes from 0 to the maximum subscript of the array.
}
"Two-dimensional" arrays:
varV1 = [2,5,1,5];varv2 = [5,1,6,8];varV3 = [8,0,9,7];varV4 =[V1, V2, V3];varV5 = [ [2,5,1,5], [5,1,6,8], [8,0,9,7]];
There is no difference between--v4 and V5, both of which can be called "two-dimensional arrays".
Operation of the "two-dimensional" array element:
Value:
var S1 = v5[0][1]; 5//is equivalent to the 2nd item in the array, which is the first item (this is an array) of V5.
var s2 = v5[2][3] + 100; 107
Assignment value:
V5[0][1] = 200;
V5[2][3] = 300;
Common methods for array objects:
What is the method: The method is actually a function! --just if a function is "subordinate" to an "object", this function is called the method of the object.
function Maibao () { document.write ("La la la, I am a small connoisseur of newspaper sellers, selling newspapers and selling newspapers.") ");} var mydreamgirl = {name: "Floret", Age:edu: "University", Sex: "Female", nengli1:function () { document.write ("Laundry! ");}, Nengli2:function () {document.write (" Cook! ");}, Nengli3:maibao}; var V1 = [2515]; var v2 = [5168];
From a strict point of view, an array is also an object-even a string is an object.
V1 as an object, there are properties and methods:
Property:
An array. Length: Indicates the length of the array object
Method:
An array. concat (other array): joins two arrays to become a new "longer" array.
var S1 = V1.concat (v2); At this point S1 is such an array: [2, 5, 1, 5, 5, 1, 6, 8];
An array. Join ("string"): All items in the array are "strung together" into "long" strings with the specified characters.
Vars2 = V1.join ("//"); Result S2 As string "2//5//1//5"
An array. Pop (); Removes the last item of the array (delete) and returns the item data, that is, the array is missing one item
var s3 = V1.pop (); The result v1 only this: [2,5,1]; The value of S3 is 5.
An array. Push (new data item D1); Adds a new data D1 to the last position of the array, that is, the array has one more item.
var S4 = V1.push (55); V1 at this point: [2,5,1, +], the value of S4 is the length of the new array, that is, 4
An array. Shift (); "Remove" the first item of the array (delete) and return the item data, that is, the array is missing one item
var S5 = V1.shift (); The result v1 only this: [5, 1,55]; The value of S5 is 2.
An array. unshift (new data item D1); Adds a new data D1 to the first position of the array, that is, the array has one more item.
var V6 = V1.unshift (66); V1 at this time: [5, 1, S6], the value of the new array is the length, that is, 4
The JavaScript language is an object-based language.
String object:
varSTR1 =NewString ("Abcdefgabc");//This is a "string object" varstr2 = "ABCDEFGABC";//This string is almost indistinguishable from the front str1 .properties of the String object:. length--gets the length of a string (that is, the number of characters) of the String object method:1. Str1.charat (n); --Get the character with the position n in the string str1 (the position of the character is also calculated from the beginning of 0)varS1 = Str1.charat (3);//The result of the S1 is: "D"2. Str1.touppercase (); --Get str1 all converted to uppercase resultsvarS2 = Str1.touppercase ();//The result of S2 is: "ABCDEFGABC"3. Str1.tolowercase (); --Get str1 all converted to lowercase resultsvarS3 = Str1.tolowercase ();//The result of S3 is: "Abcdefgabc"4. Str1.replace ("character 1", "character 2"); --Replace "character 1" in str1 with "character 2"varS4 = Str1.replace ("CD", "999”);//The result of S4 is: "AB999EFGABC"5. Str1.indexof ("character 1"); --Get the first occurrence of "character 1" in str1, if it does not appear, the result is-1varS5 = Str1.indexof ("ab");//The result of S5 is 0.6. Str1.lastindexof ("character 1"); --Get the last position of "character 1" in str1, if it does not appear, the result is-1varS6 = Str1.lastindexof ("ab");//The result of S6 is 7.7. Str1.substr (n, m)--Gets the m character starting from position N in str1, M can be omitted, then it is taken from position N to the end of the string--note that this "fetch" does not affect the original character of Str1varS7 = Str1.substr (2,4);//S7 as: "Cdef"8. Str1.substring (n, m)--Gets the first character from position N to position m in str1. varS8 = str1.substring (2,4);//S8 as: "CD"9. Str1.split ("character 1")--divides str1 into an array with the specified "character 1", resulting in an arrayvarS9 = Str1.split ("B");//The result of S9 is an array: ["A", "CDEFGA", "C"]Math Object
The Math object is a system-defined object, and we don't need to "create a new Math object"--unlike a string object and an array object. That is, the math object is used directly.
We learn math objects, nothing more than to learn some common mathematical processing functions--here is called the method:
Property:
math.pi--represents the "constant" of pi.
Method:
1. Math.max (value 1, value 2, ...)--the maximum value in a number of values is obtained. 2. Math.min (value 1, value 2, ...) --The minimum value in a number of values is obtained. 3. Math.Abs (value 1)--the absolute value of 1 is obtained4. Math.pow (x, y)-The Y-order of the value X, which is the "power operation"5. MATH.SQRT (x)--To obtain the root of x6. Math.Round (x)--The result value of rounding x is obtained;7. Math.floor (x)--the result of a downward rounding of X, that is, a maximum integer greater than x is found. A) Math.floor (3.1) ?3b) Math.floor (3.8) ?3c) Math.floor (3) ?3d) Math.floor (-3.1) ? -4e) Math.floor (-3.8) ? -48. Math.ceil (x)--the result of an upward rounding of X, which is to find a smallest integer not less than X, a) Math.floor (3.1) ?4b) Math.floor (3.8) ?4c) Math.floor (3) ?3d) Math.floor (-3.1) ? -3e) Math.floor (-3.8) ? -39. Math.random ()--Get a 0~random "Decimals" in the 1 range, including 0, but not including 1//a general practice for obtaining a random integer between two integers (N1,N2): varV1 =math.random () v1= V1 * (n2-n1+1); V1= Math.floor (v1) + N1;//Now V1 is the result .//the above 3 steps are written in one step (general formula): varV1 = Math.floor (Math.random () * (n2-n1+1)) + N1
PHP Basic Learning Notes (4)