Array array object
An array object is a collection of objects, and the objects inside can be of different types. Each member object of an array has a "subscript" to indicate its position in the array, starting from zero.
Method of array definition:
- An empty array is defined:
var array name = new Array();
2. An array of n empty elements is specified when defined:
var array name = new Array(n);
3. When defining an array, initialize the data directly:
var array name = [<Element 1>, <Element 2>, <Element 3>... ];
We define myArray arrays and assign them as follows:
var myArray = [2, 8, 6];
Explanation: An array myArray is defined. The elements inside are: myArray[0] = 2; myArray[1] = 8; myArray[2] = 6.
Array elements are used:
Array name [subscript]= value;
Note: Array subscripts are enclosed in square brackets, starting at 0.
Array properties:
Use of length: < array object > length; return: the length of the array, that is, how many elements are in the array. It is equal to the subscript of the last element in the array plus one.
Array method:
Array connection concat()
The concat() method is used to connect two or more arrays. This method returns a new array without changing the original array.
grammar
arrayObject.concat(array1,array2,...,arrayN)
Description of parameters:
Note: This method does not change the existing array, but only returns a copy of the connected array.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Array object </title>
<script type="text/javascript">
var myarr1= new Array("010")
var myarr2= new Array("-","84697581");
var myarr3=myarr1.concat(myarr2);
document.write(myarr3);
</script>
</head>
<body>
</body>
</html>
Specifies a delimiter to join the array element join()
The join() method is used to put all elements in an array into a string. Elements are delimited by a specified delimiter.
Syntax:
ArrayObject. join (separator)
Description of parameters:
Note: Returns a string that strings elements in an array and places them between elements with the <separator>. This method does not affect the original contents of the array.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Array object </title>
<script type="text/javascript">
var myarr1= new Array("86","010")
var myarr2= new Array("84697581");
var myarr3= myarr1.concat(myarr2);
document.write(myarr3.join("-"));
</script>
</head>
<body>
</body>
</html>
Reverse array element order ()
The reverse() method is used to reverse the order of elements in an array.
Syntax:
arrayObject.reverse()
Note: This method changes the original array without creating a new one.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Array object </title>
<script type="text/javascript">
var myarr1= ["I","love","you"];
document.write(myarr1.reverse());
</script>
</head>
<body>
</body>
</html>
Selected element slice()
The slice() method returns the selected element from an existing array.
grammar
arrayObject.slice(start,end)
Description of parameters:
1. Returns a new array containing elements in arrayObject from start to end (excluding that element).
- This method does not modify the array, but returns a subarray.
Be careful:
- Negative values can be used to select elements from the end of the array.
2. If the end is not specified, the slice() method selects all elements from start to the end of the array.
- String.slice() is similar to Array.slice().
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Array object </title>
<script type="text/javascript">
var myarr1= ["I","love","you"];
document.write(myarr1.slice(1));
</script>
</head>
<body>
</body>
</html>
Array sort()
The sort() method arranges elements in an array in a certain order.
Syntax:
ArrayObject. sort (method function)
Description of parameters:
1. If <Method Function> is not specified, it is arranged in unicode order.
2. If <method function> is specified, the order is arranged according to the sort method specified by <method function>.
myArray.sort(sortMethod);
Note: The function compares two values, and then returns a number indicating the relative order of the two values. The comparison function should have two parameters a and b, and its return value is as follows:
If the return value is <=-1, it means that A appears before B in the sorted sequence.
If the return value > - 1 & & & < 1, then A and B have the same sort order.
If the return value >= 1, it means that A appears after B in the sorted sequence.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Array object </title>
<script type="text/javascript">
function sortNum(a,b) {
return b-a;//Descending order
}
var myarr = new Array("80","16","50","6","100","1");
document.write(myarr.sort(sortNum));
</script>
</head>
<body>
</body>
</html>