1. Create an Array object method:
--->var arr = [element0, element1, ..., elementn];//Simple definition method
Copy the code as follows: var arr = [1,2,3]
You can know now
The code copy is as follows: arr[0] == 1;
arr[1] == 2;
arr[2] == 3;
--->new Array();
Copy the code as follows: var arr = new Array();//Define an array object with nothing, and then assign it a value in the following way
arr[0] = "arr0";
arr[1] = "arr1";
arr[2] = "arr2";
--->new Array(size);//Define an array object with a limited size, and then assign the value in the following way (the assignment method is the same as above)
The code copy is as follows: var arr = new Array(3);
arr[0] = "arr0";
arr[1] = "arr1";
arr[2] = "arr2";
--->new Array(element0, element1, ..., elementn);//Directly define the array content
The code copy is as follows: var arr = new Array("arr0","arr1","arr2");
You can know at this time:
The code copy is as follows: arr[0] == "arr0";
arr[1] == "arr1";
arr[2] == "arr2";
2. Array object properties
There are three common properties in Array: constructor, length and prototype
--->constructor, as the name suggests, is a constructor, that is, what is the object composed of, and in a more common sense, it is the type of this object, see the following example
The code copy is as follows: var arr = new Array(3);
if(arr.constructor==Array)
{
document.write("This is an Array");
}
if (test.constructor==Boolean)
{
document.write("This is a Boolean");
}
if (test.constructor==Date)
{
document.write("This is a Date");
}
if (test.constructor==String)
{
document.write("This is a String");
}
The above output is: This is an Array
--->length, that is, the length of Array
The code copy is as follows: var arr = new Array(3);
document.write(arr.length);//The output result is 3
Note that in Javascript, the properties of the Array object can be modified.
therefore:
The code copy is as follows: arr.length=5;
document.write(arr.length);//The output result is 5
--->prototype, gives you the ability to add properties and methods to objects.
The code copy is as follows: function myarray(name,age)//Define a class, which currently has two attributes
{
this.name = name;
this.age = age;
}
var myarr = new myarray("john",25);
myarray.prototype.test = null;//Added a property to myarray class
myarr.test = "test";
alert(myarr.test);//Output test
3. Concat() method --->Connect two or more arrays
It has two ways to use it:
--->Connect the actual data
example:
The code copy is as follows: var arr = new Array(1,2,3);
alert(arr.concat(4,5));//Output 1,2,3,4,5
--->Connect two or more arrays
The code copy is as follows: var arr1 = new Array(1,2,3);
var arr2 = [4,5];
var arr3 = new Array("jone","john");
alert(arr1.concat(arr2,arr3));//Output 1,2,3,4,5,jone,john
4. Join() method--->Elements in the array are placed in a string
It can have parameters or no parameters, and the parameters represent the way of segmenting the generated string
--->No registry
The code copy is as follows: var arr = new Array("jone","Grrgy","john");
alert(arr.join());//Output jone,Grrgy,john string is separated by
--->Get ginseng
The code copy is as follows: var arr = new Array("jone","Grrgy","john");
alert(arr.join("."));//Output jone.Grrgy.john The middle of the string is separated by parameters
5. The pop() method is used to delete and return the last element of the array (before deletion)
The code copy is as follows: var arr = new Array("jone","john","grrgy");
document.write(arr.pop());//Output content: grrgy
document.write(arr.join("-"));//Output: jone-john
6. The push() method is used to add an element to the last array and return the length of the array (after adding)
If the parameter in push() is empty (not filled in), the original length of the array will be returned and no modifications will be made to the array.
example:
The code copy is as follows: var arr = ["jone","john","grrgy"];
document.write(arr.push("tom"));//Output: 4 (length)
document.write(arr.join());//Output: jone,john,grrgy,tom
7. reverse() reverse() reverses the order of elements in the array without parameters
example:
The code copy is as follows: var arr = ["jone","john","grrgy"];
document.write(arr.reverse());//grrgy,john,jone
8. shift() deletes and returns the first element of the array (before deletion)
The code copy is as follows: var arr = ["jone","john","grrgy"];
document.write(arr.shift());//Output: jone
document.write(arr.join());//Output: jone,john
9. slice() returns the specified element from the specified array. Note: it returns an array
It has two parameters, start and end.
Start is required, specifying the location of the starting element
end is optional, specify the position of the end element. If not written, it is considered to be at the end of the array.
example:
The code copy is as follows: var arr = ["jone","john","grrgy","tom","hell"];
var test = arr.slice(1);
if(test.constructor==Array)
{
document.write("This is an Array<br>");
document.write(test.join());
}
The final result output:
This is an Array
john grrgy tom hell
If you change var test = arr.slice(1) to:
var test = arr.slice(1,2);
The result output is:
john
10. sort() sorts the elements of an array, a very important method
It can have parameters, the parameter is a function(), which specifies the ordering rules,
Note, it generates a copy of the original array and will not generate a new array, that is, it will be modified based on the original array.
If no parameters are added, it will be followed by the built-in sorting method in Javascript, alphabetical order
example:
The code copy is as follows: var arr = ["jone","john","grrgy","tom","hell"];
document.write(arr.sort());
document.write("<br>");
document.write(arr);
The output result is:
grrgy,hell,john,jone,tom
grrgy,hell,john,jone,tom
Below is sorted by number size
Copy the code as follows: function sortNumber(a,b)// Function that defines the sorting rules
{
if(a>b)
{
return 1;
}
else if(a<b)
{
return -1;
}
else
{
return 0;
}
}
var arr = new Array(1,2000,3,400);
document.write(arr.sort(sortNumber));// Only the function name can be written here
document.write("<br>");
document.write(arr);
Output:
1,3,400,2000
1,3,400,2000
11. splice() deletes elements and adds elements to the array
splice(index, howmany, element1, element2......elementx) is explained as follows:
index is required, specifying where elements are added/deleted. This parameter is the subscript of the array element that begins to be inserted and/or deleted and must be a number.
howmany is required. Specifies how many elements should be deleted. Must be a number, but can be "0". If this parameter is not specified, all elements from index to end of the original array are deleted.
When howmany is 0, it means that no elements are deleted, which means that only add
element1 is optional, specifying the new element to be added to the array. Start inserting from the subscript indicated by index, multiple can be inserted
The difference between splice() and slice() is that splice() is a processing of the original array. It modifies the value of the original array and returns an array.
splice() is equivalent to replacing an element in the array, or inserting or deleting it.
See the following three examples:
--->Insert only
The code copy is as follows: var arr = new Array(6);
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
arr[3] = "James";
arr[4] = "Adrew";
arr[5] = "Martin";
document.write(arr + "<br />");
arr.splice(2,0,"William");
document.write(arr + "<br />");
Output result:
George, John, Thomas, James, Adrew, Martin
George, John, William, Thomas, James, Adrew, Martin
William inserts into position 2
--->Delete only
The code copy is as follows: var arr = new Array(6);
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
arr[3] = "James";
arr[4] = "Adrew";
arr[5] = "Martin";
document.write(arr + "<br />");
arr.splice(2,1);
document.write(arr + "<br />");
Output result:
George, John, Thomas, James, Adrew, Martin
George, John, James, Adrew, Martin
Deleted the element in the original array 2 position
--->Delete and add (equivalent to replacement)
The code copy is as follows: var arr = new Array(6);
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
arr[3] = "James";
arr[4] = "Adrew";
arr[5] = "Martin";
document.write(arr + "<br />");
arr.splice(2,1,"William");
document.write(arr + "<br />");
Output result:
George, John, Thomas, James, Adrew, Martin
George, John, William, James, Adrew, Martin
Replace the original Thomas with William
12. toSource() returns the source code of the object. This method is usually called automatically in the background of Javascript. It is rarely used in the foreground and this method cannot be implemented in the IE browser. Example: in firefox
The code copy is as follows: var myarr = new Array('lisi',25);
document.write(myarr.toSource());
The output result is:
["lisi", 25]
If you redefine a class, you can display the attribute name, for example:
The code copy is as follows: function myarray(name,age)
{
this.name = name;
this.age = age;
}
var myarr = new myarray('lisi',25);
document.write(myarr.toSource());
The output result is:
({name:"lisi", age:25})
A bit similar to Json type data, but it is just similar. It is not a Json data type format
13. toString(), the array is returned into a string, which is the same as the result of join() implementation, but the join() method can customize the interval symbols while toString() cannot, and can only be separated by, for example:
The code copy is as follows: var myarr = new Array('jone','john','Tom');
document.write(myarr.join('.'));
document.write('<br>');
document.write(myarr.join(','));
document.write('<br>');
document.write(myarr.join());
document.write('<br>');
document.write(myarr.toString());
The output result is:
jone.john.Tom
jone, john, Tom
jone, john, Tom
jone, john, Tom
It can be seen that the results of the last three methods are the same
14. unshift(), you can add one or more elements to the beginning of the array and return the new length of the array, and the original array will change.
The code copy is as follows: unshift(element1, element2, element3......), at least one element, example:
var myarr = new Array('jone','john','Tom');
var length = myarr.unshift('zhangsan','lisi');
document.write(myarr);
document.write('<br>');
document.write(length);
The output result is:
zhangsan,lisi,jone,john,tom
5
To view more JavaScript syntax, you can follow: "JavaScript Reference Tutorial" and "JavaScript Code Style Guide". I also hope that everyone will support Wulin.com more.