1. Create an Array object method:
--->var arr = [1,2,3];//Simple definition method
You can know now
arr[0] == 1;arr[1] == 2;arr[2] == 3;--->new Array();var arr = new Array();//Define an array object with nothing, and then assign a value to it in the following way arr[0] = "arr0";arr[1] = "arr1";arr[2] = "arr2";--->new Array(size);//Define an array object with a size limitation, and then assign a value in the following way (the assignment method is the same as above) var arr = new Array(3);arr[0] = "arr0";arr[1] = "arr1";arr[2] = "arr2";--->new Array(element0, element1, ..., elementn);//Directly define the array content var arr = new Array("arr0","arr1","arr2");You can know at this time:
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
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 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:
arr.length=5;document.write(arr.length);//The output result is 5--->prototype, which gives you the ability to add properties and methods to the object. function myarray(name,age)//Define a class. This class currently has two attributes {this.name = name;this.age = age;}var myarr = new myarray("john",25);myarray.prototype.test = null;//Add a property for myarray class myarr.test = "test";alert(myarr.test);//Output test3. Concat() method --->Connect two or more arrays
It has two ways to use it:
--->Connect the actual data
example:
var arr = new Array(1,2,3);alert(arr.concat(4,5));//Output 1,2,3,4,5
--->Connect two or more arrays
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,john4. 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 parameter var arr = new Array("jone","Grrgy","john");alert(arr.join());//Output jone,Grrgy,john The middle of the string is separated by,-->The parameter var arr = new Array("jone","Grrgy","john");alert(arr.join("."));//Output jone.Grrgy.john The middle of the string is separated by parameters5. The pop() method is used to delete and return the last element of the array (before deletion)
var arr = new Array("jone","john","grrgy");document.write(arr.pop());//Output content: grrgydocument.write(arr.join("-"));//Output: jone-john6. 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:
var arr = ["jone","john","grrgy"];document.write(arr.push("tom"));//Output: 4 (length) document.write(arr.join());//Output: jone,john,grrgy,tom7. reverse() reverse() reverses the order of elements in the array without parameters
example:
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)
var arr = ["jone","john","grrgy"];document.write(arr.shift());//Output: jonedocument.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:
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:
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
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 write the function name 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 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 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 replacing) 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 and is rarely used in the foreground.
And this method cannot be implemented in IE browser, for example: in firefox
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:
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.
But toString() cannot be used, it can only be separated by, for example:
var myarr = new Array('jone','john','Tom');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.
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
The above is the relevant knowledge about Array objects (array objects) in JavaScript introduced to you by the editor. I hope it will be helpful to everyone!