How to quickly get started with VUE3.0: Enter learning
Related recommendations: JavaScript tutorial
1. Common two-dimensional array operations
In the previous chapters, you have learned various ways to create one-dimensional arrays. After understanding how to create one-dimensional arrays, Creating a two-dimensional array is very simple, just set the array elements to an array.
After creating a two-dimensional array, how to traverse the elements in the two-dimensional array and operate on them?
In addition, in web project development, multi-dimensional arrays are often created by adding elements to multi-dimensional empty arrays. The following demonstrates adding a two-dimensional empty array element as an example.
If you want to assign a value to a two-dimensional array element (such as arr[i][0]), you must first ensure that the added element (such as arr[i]) has been created as an array, otherwise the program will report an "Uncaught TypeError..." error.
Note that
when creating a multi-dimensional array, although JavaScript does not limit the number of array dimensions, in actual applications, in order to facilitate code reading, debugging, and maintenance, it is recommended to use three-dimensional or less arrays to save data.
[Case] Transpose of two-dimensional array
The transposition of a two-dimensional array refers to saving the horizontal elements of the two-dimensional array into vertical elements.
Code implementation ideas:
In order to give you a sense of accomplishment, I won’t post the code. If you have any questions, you can ask them in the comment area. In fact, the matrix can be stored in an array. In the future, you can just run the code directly by transposing the matrix.
2. Common array method
In JavaScript, in addition to the methods of adding and deleting array elements explained earlier, you can also use the methods provided by the Array object to simulate stack and queue operations.
is being developed to detect whether a given value is an array, or to find the position of a specified element in an array.
Except for the Array.isArray() method, all other methods in the table start retrieval from the specified array index by default, and the retrieval method is the same as the operator "===", that is, only when they are congruent will a more successful result be returned. .
includes() and Array.isArray() methods
The indexOf() method
indexOf() is used to retrieve the first given value from the specified subscript position in the array. If it exists, it returns the corresponding element subscript, otherwise it returns -1.
Note that
the second parameter of the indexOf() method is used to specify the index to start searching:
lastIndexOf() method
The lastIndexOf() method provided by the Array object is used to retrieve the subscript of the last given value in the array from the specified subscript position. Different from the indexOf() retrieval method, the lastIndexOf() method defaults to reverse retrieval, that is, retrieval from the end of the array to the beginning of the array.
Note that
the second parameter of the lastIndexOf() method is used to specify the search index, and because it uses a reverse search method:
when its value is greater than or equal to the length of the array, the entire array will be searched.
When its value is a negative number, the index position is equal to the length of the array plus the given negative number. If its value is still a negative number, -1 is returned directly.
If you need to convert an array into a string during development, you can use the methods provided by JavaScript to achieve this.
The same points between the join() and toString() methods:
The difference between the join() and toString() methods:
In addition to the several common methods explained earlier, JavaScript also provides many other commonly used array methods. For example, merge arrays, shallow copy arrays, reverse the order of array elements, etc.
Note that
Related recommendations: JavaScript tutorial
The above is the details of common array methods in JavaScript and teaches you how to transpose a matrix. For more information, please pay attention to other related articles on the PHP Chinese website!