Knowledge about JS arrays and two-dimensional arrays (very easy to use, a look at the value)

JS array related knowledge

Here is a detailed explanation of the related knowledge of the array, and try to express it so that everyone can see and understand it. Everyone looks carefully and actually operates it. It is not particularly difficult to understand. Don't be afraid to see the array. As long as you understand it carefully and practice more, I believe you will be able to master the knowledge of arrays very well. There are many methods in it. Because of the limited time, I will not demonstrate them one by one. You can try out the function of each method more, write a note, and use it later when you need it.
1. Array introduction:
Array: is a collection of a set of data, each data in the array is called an element.
Array elements can be of any type, and different elements in the same array may be objects or arrays (multiple data types are available in js In the same array)
1. Each array has a length attribute, which is the number of array elements (array length)

2. Each element in the array has a position in the array, represented by a number, called an index (subscript)

3. The index starts from 0, [0] represents the first data, and so on

4. The largest index of the array is always 1 less than the length of the array

5. The data type of the array is an object, which is actually a special object with numeric index and length attribute added.
Insert picture description here

2. Array creation method: The length of the array can be modified through the array name.length=value (number), or you can directly assign a value of 0 to it, which means to clear the array.
1. The most common way to create an array implicitly (recommended to use this way)
var arr=["小q","小w","小e","小r"];
2. Direct instantiation , Through the function Array(), Array() can give a parameter in the parentheses to indicate the length of the created array.
var arr=new Array("small q", "small w", "small e", "small d");
3. Create an array and assign values ​​to the array elements, and assign values
via subscript var arr=new Array();
arr[0]="small q";
arr[1]="small w";
arr[2]="small e";
arr[3]="small r";

3. Modification and addition of the array (adding through the subscript of the array, the most important thing in the array is to understand the subscript of the array. It can also be called the index)
a. Modification of the data in the array: directly use the subscript to re-assign the array elements
. : Arr[2]="Xiaoming";
b. The data in the array is increased (it is enough if the subscript is larger than the original array length)

Example: var arr=["small q","small w","small e","small d"];

The first way to add:
arr[4]="small r";
arr[5]="small t";
arr[6]="small y";

The second way to add:
arr[arr.length]="small k";
arr[arr.length]="small y";

4. Traverse the array:
Because the value of the array is accessed through the subscript, if we want to get the value of the array element, we traverse the array and get the corresponding subscript to get the corresponding value.
For example: an existing array: var arr=["小q","小w","小e","小d"];

第一种:普通for
for (var i=0;i<4;i++) {
document.write(arr[i]+"
");
}

The second type: a for loop using the length of the array (because the array has four elements at this time, so arr.length=4)
for (var i=0;i<arr.length;i++) { document.write(arr[ i]+" "); }


The third type: optimized for loop
var len=arr.length;
for (var i=0;i<len;i++) { document.write(arr[i]+" "); }


The fourth type: for-in traversal, using subscripts
. There are many people who use it, and the efficiency of all traversal methods is the lowest (if the amount of data is relatively small, this method is good)
for (i in arr) { document.write(arr [i]+" "); }


The fifth type: for-of traversal (ES6), using data, for-of traversal is more efficient than for-in traversal, but lower than ordinary for loops
for (value of arr) { document.write(value+ "); }


5. Array example: Find the maximum and minimum values ​​(you can try to find the average of the array, sum, and output in reverse order, as long as you make good use of the array subscripts, these can be implemented very simply) The
code is as follows:

Insert picture description here
6. Two-dimensional array: In fact, a two-dimensional array is also an array, but the original one-dimensional array contains a single element, and the two-dimensional array contains an array, which is equivalent to a table in the form of nested arrays Array.
Insert picture description here
1. The establishment of a two-dimensional array:
var arr=[ [27,54,66],[62,55,75],[91,25,36], ];

Traversal of two-dimensional arrays:
Insert picture description here
7. Commonly used methods in arrays: (emphasis on mastering)
1. Array name .join(); a parameter (separator) can be placed in the brackets, and the default is a comma if it is not written.
Convert the array element to a string, the original array will be retained.
2. Array.from() converts a pseudo-array into an array, as long as it has a length attribute, it can be converted into an array. The parameters in the brackets can be parameters that need to be converted into an array, such as passing in a string.
Insert picture description here
3.sort() is used to sort the elements of the array (from small to large).
Usage: array
name.sort (); 4.pop(); method removes the last item from the end of the array, reduces the length value of the array, and then returns the removed element, which will directly modify the original array instead of creating a new one Array.
Usage: array
name.pop (); 5.push() is used to add one or more elements to the end of the array, and return the length of the modified array (numerical type), which will directly modify the original array instead of creating a new one The array.
Usage: array
name.push (); 6.shift() Delete the first item at the beginning of the array, reduce the length value of the array, and then return the removed element, which will directly modify the original array instead of creating a new array.
Usage: array name.shift();
7.unshift() is used to add one or more elements to the beginning of the array and return the length of the modified array (numerical type), which will directly modify the original array instead of creating a new one The array.
Syntax: array name.unshift();
8.splice(): delete, insert, replace and intercept. Will directly modify the original array
1. Delete: Any number of items can be deleted, and 2 parameters need to be specified: the position of the first item to be deleted and the number of elements to be deleted.
2. Insert: You can insert any number of items into the specified position, and you need to provide at least 3 parameters: 1. Start position, 2. (Number of items to be deleted 0) and items to be inserted (there can be more than one).
Example:
Insert picture description here
9.slice(): intercept the array or string elements of the specified subscript. Both array and string are valid, and the original array or string will not be modified.
Syntax: slice (start subscript, end subscript);
if the end subscript is not specified, then all elements from the start subscript to the end of the array will be selected and
returned A new array or string containing from the start index to the end index (not including the end index index).
Negative values ​​can be used to select elements from the end of the array (from left to right)
10.indexOf() Find the index of the specified element ,Search from front to back.
If there are duplicates, return the first index found. If not, return -1.
Usage: array name.indexOf(); The parameter in parentheses is the element to be searched.
11.lastIndexOf() Find the index of the specified element, search from back to front.
If there are duplicates, return the first index found.
If not, return -1.
Usage: array name.lastIndexOf(); brackets The parameter inside is the element to be found.
12. The concat() method of merging arrays is used to connect two or more arrays.
This method will first create a copy of the current array, without changing the original array, and will return a new array.
Usage: Array name 1. Merge concat (array that needs to be merged);
Use of 12set data type
It is similar to an array, but the values ​​of the members are unique and there is no duplicate value.
Define an array: var arrOne = [5,1,5,2,1,3];

1. Array to set
var setarr = new Set(arrOne); Return [1,2,3]
2. Set to Array The
first method:
var arrTwo = Array.from(setarr); Return [1,2,3]
The second method:
var arrThree = […setarr]; returns [1,2,3]

3.has() determines whether a value is
returned in the set to a boolean value, which is true if it is not false.
Usage: set data type variable name.has (value to be judged);

Of course, the knowledge here is not particularly comprehensive, but when we learn a programming language, we don’t have to master all the methods, because there are too many methods and knowledge we can’t remember so much, we must make good use of Baidu at this time. When we need this function, we search for the corresponding knowledge points, because we have a foundation, it is also very easy to understand.

Guess you like

Origin blog.csdn.net/m0_46188681/article/details/106073651
Recommended