Properties and methods of arrays
Property
Sets or returns the length of an array ary.length
Empty array: ary.length = 0;
Method (ary. method)
1. Add, delete
1) Add Ary.push at the end (added elements, multiple uses are well separated); Returned is the length
1) The opening increases ary.unshift (added elements, multiple are separated with good); Returned is the length
2) Delete Ary.pop () at the end; Only one element at the end can be deleted, and the deleted element is returned.
2) Start delete ary.shift (); Only one element at the end can be deleted, and the deleted element is returned.
3) Delete or add elements at the specified location
Ary.splice (the subscript of the position, the number of elements removed, the element added, the element added, and the elements added);
The first two parameters must have
2. Find
1) ary.includes (' a '); Determine if the element exists in the array
2) Ary.indexof (' a '); Find the position of ' a ' in the array and return to the subscript
3) Ary.lastindexof (' a '); Find ' A ' in the last position in the array, return to subscript
Use IndexOf (' a ') and LastIndexOf (' a ') to determine whether the element is unique in the array.
3. Intercepting a new array (slice)
Ary.slice (start subscript, end subscript);
A new array is formed as part of the Intercept array, and the new array is truncated from the beginning, and truncated to the previous element of the end subscript.
4. Splicing of array elements
Ary.join (' Symbols for stitching ');
Each element of an array is stitched together by a specified symbol to form a new string
5. Sorting (sort)
Ary.sort (function (b) {
return b-a; Sort from big to small
});
Traversal of an array
1. Iterating through the array with a For loop
for (Let I=0;i < ary.length; i++) {
Console.log (Ary[i]); Each item that corresponds to an array of small marks
}
2.for...of traversing an array
For (let item of ary) {
Console.log (item); Each item in the array
}
3.forEach method Traversal Array
Ary.foreach (function (item,index) {
Console.log (Item,index); Item must be filled in, index optional
});
The knowledge points commonly used in arrays array in JS