Detailed Explanation of Array Array Object of javascript
- 2021-06-28 06:14:58
- OfStack
1. Create an Array object method:
---
>
var arr = [element0, element1,..., elementn]; //Simple method of definition
var arr = [1,2,3]
At this point, you can know
arr[0] == 1;
arr[1] == 2;
arr[2] == 3;
---
>
new Array();
var arr = new Array();// Definition
1 An array object with no content, 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 limited size, and then assign it in the following way (assign it in the same way as above)
var arr = new Array(3);
arr[0] = "arr0";
arr[1] = "arr1";
arr[2] = "arr2";
---
>
new Array (element0, element1,..., elementn); //Define the contents of the array directly
var arr = new Array("arr0","arr1","arr2");
At this point, you can know:
arr[0] == "arr0";
arr[1] == "arr1";
arr[2] == "arr2";
2. Array object properties
There are three common attributes of Array: constructor, length, and prototype
---
>
constructor, as its name implies, is a constructor, that is, what is the composition of this object, and then the popular point 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 is
3
Note that the properties of the Array object can be modified in Javascript,
Therefore:
arr.length=5;
document.write(arr.length);// The output is
5
--- > prototype, giving you the ability to add properties and methods to objects.
function myarray(name,age)// Definition
1 Class, which currently has two attributes
{
this.name = name;
this.age = age;
}
var myarr = new myarray("john",25);
myarray.prototype.test = null;// For
myarray Class adds the
1 Attributes
myarr.test = "test";
alert(myarr.test);// Output
test
3. concat () method--
>
Connecting two or more arrays
It can be used in two ways:
---
>
Connect actual data
Example:
arr[0] == 1;
arr[1] == 2;
arr[2] == 3;
0
---
>
Connecting two or more arrays
arr[0] == 1;
arr[1] == 2;
arr[2] == 3;
1
4. join () method--
>
The elements in the array are put into a string
It can have parameters or no parameters, and the parameters represent the partition of the generated string
---
>
No parametric
var arr = new Array("jone","Grrgy","john");
alert(arr.join());// Output
jone,Grrgy,john In the middle of the string,
, Separate
---
>
Have reference
var arr = new Array("jone","Grrgy","john");
alert(arr.join("."));// Output
jone.Grrgy.john Strings are separated by parameters
5. The pop () method is used to delete and return the last element of the array (before deletion)
arr[0] == 1;
arr[1] == 2;
arr[2] == 3;
4
6. The push () method is used to add 1 element to the end of the array and return the length of the array (after addition)
If the parameter in push () is empty (not filled in), the original length of the array is returned without any modification to the array
Example:
arr[0] == 1;
arr[1] == 2;
arr[2] == 3;
5
7. reverse () reverses the order of the elements in the array without a parameter
Example:
arr[0] == 1;
arr[1] == 2;
arr[2] == 3;
6
8. shift () deletes and returns the first element of the array (before deletion)
arr[0] == 1;
arr[1] == 2;
arr[2] == 3;
7
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, specifying the position of the ending element, which is considered to the end of the array if it is not written
Example:
arr[0] == 1;
arr[1] == 2;
arr[2] == 3;
8
Final result output:
This is an Array
john,grrgy,tom,hell
Replace var test = arr. slice (1) with:
var test = arr.slice(1,2);
The result output is:
john
10. sort () is a very important way to sort the elements of an array
It can have arguments that are 1 function (), which specifies the rules for sorting,
Note that it produces a copy of the original array, and does not generate a new array, that is, it is modified on the basis of the original array
If no parameters are added, it will be sorted according to the built-in sorting method in Javascript, alphabetical order
Example:
arr[0] == 1;
arr[1] == 2;
arr[2] == 3;
9
The output is:
grrgy,hell,john,jone,tom
grrgy,hell,john,jone,tom
The following is sorted by number size
var arr = new Array();// Definition
1 An array object with no content, and then assign a value to it in the following way
arr[0] = "arr0";
arr[1] = "arr1";
arr[2] = "arr2";
0
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 described as follows:
index is required and specifies where elements are added/removed. This parameter is the subscript of the array element to start inserting and/or deleting, and must be a number.
howmany is required. Specify 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 the end of the original array are deleted.
When howmany is 0, it means that no elements are deleted, and the implication is that only elements are added
element1 is optional and specifies new elements to be added to the array. Insert from the subscript referred to by index, and multiple can be inserted
The difference between splice () and slice () is that splice () is processing the original array, modifying the value of the original array and returning one array.
splice () is equivalent to replacing, inserting or deleting an element in an array
Look at 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:
George,John,Thomas,James,Adrew,Martin
George,John,William,Thomas,James,Adrew,Martin
William is inserted 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:
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 replace)
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:
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 1 is generally located in the background of Javascript, which is automatically called. It is rarely used in the foreground and this method cannot be realized in IE browser. For example, in firefox
var myarr = new Array('lisi',25);
document.write(myarr.toSource());
The output 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 is:
({name:"lisi", age:25})
It is somewhat similar to the data of Json type, but it is only similar. It is not an Json data type format
13. toString (), the array is returned as a string, which is the same as the result implemented by join (), but the join () method can customize the symbol of the interval while toString () cannot, and 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.join());
document.write('<br>');
document.write(myarr.toString());
The output is:
jone.john.Tom
jone,john,Tom
jone,john,Tom
jone,john,Tom
It can be seen that the results of the latter three methods are one
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 be changed
var arr = new Array();// Definition
1 An array object with no content, and then assign a value to it in the following way
arr[0] = "arr0";
arr[1] = "arr1";
arr[2] = "arr2";
7
The output is:
zhangsan,lisi,jone,john,Tom
5