The concept of arrays
An array is a set of numbers (generally of the same data type) that define many variables of the same data type at once. If a single variable is a small container, an array is a large container with multiple grids (each grid is a small container). Var arr = [1,2,3]; var arr = [1,2,3]; (Literal declaration) array subscript —- Method of accessing the value of an array: arr[index] Note: Subscripts start at 0 Array length —-arr.length (can be changed at any time) example:
var arr = [1.2.3];console.log(arr[10]); // arr[10] is not assigned, undefined
/ / [1, 2, 3,,,,,,, 20]
// Actually lengthen the array
arr[10] = 20 ;
console.log(arr[10]);
console.log(arr.length); // The length of the array is changed
Copy the code
Array declaration
- Literal declaration:
Var arr = [1, 2, 3]
- Instantiate an object:
Var arr = new Array(1,2,3);
Array constructor (used to create objects), which represents the length of an Array if the Array has only one argument.
For example: declare an array to store 10 numbers:
// Define an empty array
var arr = [];
for (var i = 0; i < 10; i++) {
/ / in the value
arr[i] =i ;
}
console.log(arr)/ / print,1,2,3,4,5,6,7,8,9 [0]
Copy the code
Array traversal
Traversal: Reads every value in an array. Normal loop traversal:
// Ordinary loops read each value in the data
for(var i = 0 ; i < arr.length ; i++) {
console.log(arr[i]);
}
Copy the code
For in(Corner markers for traversing objects –bug (automatically filter null values))
for inHorn markers for traversing objects -- bug(automatically filters out null values)for(var i in arr) {
console.log(arr[i]);
}
Copy the code
For of (iterating over the value of the object)
for (var v of arr) {
console.log(v);
}
Copy the code
Application: find the maximum value in the array: idea is, equivalent to false try, define a value first, take him as the maximum value, and then compare with the value in the array one by one, larger than his words, large value assigned value assigned to him, until the comparison is finished.
<script>
// Encapsulate a function to find the maximum value of an array. Arr is an array
function ismax(arr) {
// The idea is to try to define a value as the maximum value and then compare it to the values in the array
for (var i = 0, max = 0; i < arr.length; i++) {
if(max < arr[i]) { max = arr[i]; }}return max
}
console.log(ismax([1.2.4.5.6.3]));
</script>
Copy the code
Basic methods of arrays:
push(v1,v2,v3…) Unshift () appends a value to the head of the array shift() appends a value to the head of the array. In general, arguments can be used to accept the values of arguments, but arguments can be used to accept arguments such as the method push that encapsulates the array described above:
var arr = [1.2.3.4.5]
// Encapsulate push, append values to the end of the array
function push() {
Arr. Length (argument.length); //arguments Then you can store values
for (var i = 0; i < arguments.length; i++) {
arr[arr.length] = arguments[i];
}
console.log(arr);
}
push(6.7.'hello'.'hi')/ / [1,2,3,4,5,6,7, 'hello,' hi ']
Copy the code
The method that wraps the above array pop:
var arr = [1.2.3.4.5]
// Wrap pop, remove a value at the end of the array
function pop() {
// Define an empty array arr1
var arr1 = [];
// Iterate over the arR array that removes the last number and adds it to the empty array
for (var i = 0; i < arr.length - 1; i++) {
arr1.push(arr[i])
}
console.log (arr1)} pop ()/ / [1, 2, 3, 4]
Copy the code
The method that encapsulates the above array is unshift:
var arr = [1.2.3.4.5]
// Encapsulate unshift, append to the header of the array
function unshift() {
var arr3 = [];
for (var i = 0; i < arguments.length; i++) {
arr3[arr3.length] = arguments[i];
}
for (i = 0; i < arr.length; i++) {
arr3.push(arr[i])
}
console.log(arr3)
}
unshift('a'.'b'.8)/ / / 'a', 'b', 8,1,2,3,4,5]
Copy the code
The shift method that encapsulates the array described above:
var arr = [1.2.3.4.5Wrap shift to remove a value from the array headerfunction shift() {
var arr2 = [];
// Iterate over the array arr that removes the first number
for (var i = 1; i < arr.length; i++) {
arr2.push(arr[i])
}
console.log(arr2)
}
shift()/ / 5-tetrafluorobenzoic [2]
Copy the code
Array sort:
- Bubble sort
- Selection sort
I believe that this is a lot of small white’s first small difficulty, alone with a blog