The target
- Know why you have arrays (multiple related elements)
- Ability to create arrays (two ways)
- The ability to get elements of an array (arR [subscript])
- Ability to iterate over arrays (for loop)
- Can add an element to an array
- Can independently complete bubble sort cases
directory
- The concept of arrays
- Create an array
- Gets the elements of an array
- Through the array
- New element in array
- An array of cases
1. The concept of arrays
What is an array
A: An array is a collection of data, each of which is called an element. An array can hold any type of element. Arrays are an elegant way to store a set of data under the name of a single variable.
2. Create an array
There are two ways to create arrays in JS:
- Create an array using new
- Create arrays with array literals (common)
- Array literals are square brackets []
- Declaring an array and assigning a value to it is called array initialization
- Arrays can hold any type of data, such as strings, numbers, booleans, etc.
var arr = new Array(); // Create an empty arrayCopy the code
var arr = []; // Create an empty arrayCopy the code
3. Get the array element
3.1 Index of array
Index (subscript) : The index used to access the elements of an array (array subscripts start at 0).
An array can be accessed, set, or modified by an index. We can obtain the elements of an array by name.
Access here means get
4. Go through the numbers
How do you get them all out?
As you can see from the code, the code repeats as each element is fetched from the array, except that the index values are increasing
The answer is loop
Traversal: The process of going through each element of the array once (similar to our morning roll call).
Var arr = [6]; for (var i =0; i<6; i++) { console.log(arr[i]); }Copy the code
4.1 Array length
Array name.length is used to access the number of elements in the array (array length).
Dynamically monitor the number of array elements
Exercise: Find the largest value in an array
Set a Max value, take the first element and compare it with the next element
Specific steps:
- Declare a variable Max that holds the largest element.
- The default maximum value can be the first element in the array.
- Iterate through the array and compare each element to Max.
- If the array element is greater than Max, store the array element in Max, otherwise proceed to the next round of comparison.
- Final output Max
Var arr =,6,1,77,52,25,7 [2]; var max=arr[0] ; for (var i=1 ; i <arr.length; i++) { if(max <arr[i] ) { max = arr[i]; }} console.log(' Max is '+ Max);Copy the code
5. Add an element to the array
You can add array elements by length and index number
5.1 Adding an array element by Changing the Length
- You can modify the length to expand the array
- The length attribute is read-write
The default value of a declared variable is undefined.
5.2 Adding an Array Element by modifying an Array index
- You can append array elements by changing the array index
- You cannot assign to the name of the array directly, otherwise the previous data will be overwritten
Practice finding numbers greater than 10
The first way is to declare a variable j, and let the variable j++
Var arr = [2, 0, 6,1,77,0,52,0,25,7]; var newarr = []; var j =0 ; for (var i = 0 ; i < arr.length ; i++) { if ( arr[i]>=10) { newarr[j] = arr[i]; j++; } } console.log(newarr);Copy the code
Second, use newarr.length
newarr[newarr.length] = arr[i];
Copy the code
The key is that the new array index starts at 0
6. Array cases
6.1 Deleting a specified array element
Requirement: remove the zeros from the array [2,0,6,1,77,0,52,0,25,7] to form a new array that does not contain zeros.
Simple array de-duplication
Analysis:
- A new array is required to hold the filtered data.
- Iterate through the original array and add data that is not 0 to the new array (remember to accept data in the array name + index format).
- The number in the new array is incremented by length.
Var arr =,0,6,1,77,0,52,0,25,7 [2]; var newarr = []; for (var i = 0 ; i<arr.length ; i++) { if (arr[i] ! =0 ){ newarr [ newarr.length]=arr [i]; } } console.log(newarr);Copy the code
6.2 Flipping an Array
Requirement: Store the contents of array [‘red’, ‘green’; ‘blue’, ‘pink ‘, ‘Purple ‘] in reverse.
Output :[‘purple’, ‘pink ‘; ‘blue’, ‘green’; ‘red’]
Ideas:
Reverse the contents of array [“red ‘, ‘green’, ‘blue’, ‘pink ‘, ‘Purple ‘]
- Declare a new array, newArr
- Return the 4th element of the old array index (arr.leng-1) and the 0th element of the new array index (newarr.length).
- We’re going to go down I —
Var arr =,0,6,1,77,0,52,0,25,7 [2]; var newarr = []; for (var i = arr.length-1 ; i>=0 ; i--) { newarr[newarr.length]= arr[i] ; } console.log(newarr);Copy the code
6.3 Bubble Sort
Bubble sort: is an algorithm that displays a series of data in a certain order (from smallest to largest or from largest to smallest).
For example, we can sort the elements in the array [5,4,3,2,1] from smallest to largest, with output: 1,2,3,4,5
- So the total number of trips we need to take is the outer for loop
Array length minus 1 arr. Length – 1 2. So the number of swaps per trip we use the inner for loop
There are four exchanges on the first trip
The second trip is three times
On the third trip, I switch twice
There will be one exchange on the fourth trip
The length is the length of the array minus the number of times, but our number of times starts at zero, so eventually arr. Length -i -1 3. Just swap two variables
var arr = [5, 4, 3, 2, 1]; for (var i = 0; i <= arr.length - 1; I++) {// for (var j = 0; j <= arr.length - i - 1; If (arr[j] > arr[j + 1]) {var temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } console.log(arr);Copy the code