Home > Article > Web Front-end > Common JavaScript array methods and teach you how to transpose a matrix
This article brings you relevant knowledge about JavaScript. It mainly introduces common array methods and teaches you how to transpose matrices, including creation and traversal, stacks and queues, retrieval methods, etc. I hope everyone has to help.
Related recommendations: javascript tutorial
1. Common two-dimensional array operations
In the previous chapter, we have learned various ways to create one-dimensional arrays. After understanding how to create one-dimensional arrays, the creation of two-dimensional arrays is very simple. Just add Just set the array elements to an array.
After creating the two-dimensional array, how to traverse the elements in the two-dimensional array and operate on it?
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.
To assign a value to a two-dimensional array element (such as arr[i][0]), first ensure that the added element (such as arr[i]) has been created It is an array, otherwise the program will report an "Uncaught TypeError..." error.
Note
When creating a multi-dimensional array, although JavaScript does not limit the dimensions of the array, in actual applications, in order to facilitate code reading, debugging and maintenance, it is recommended Use arrays of three dimensions and below to save data.
[Case] Transpose of a two-dimensional array
The transposition of a two-dimensional array refers to saving the horizontal elements of the two-dimensional array as 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 methods
In JavaScript, in addition to the previously explained methods of adding and deleting array elements In addition, you can also use the methods provided by the Array object to simulate stack and queue operations.
In development, you want to detect whether the given value is an array, or to find the position of the specified element in the array.
Except for the Array.isArray() method, the other methods in the table start retrieval from the position of the specified array index by default, and the retrieval method is the same as the operator "=== ” are the same, that is, a more successful result will be returned only when they are congruent.
includes() and Array.isArray() methods
indexOf() method
indexOf() is used to retrieve the first given value from the specified subscript position in the array. The corresponding element subscript is returned, otherwise -1 is returned.
Note
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 last index from the specified subscript position in the array. The subscript of a fixed value. 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
The second parameter of the lastIndexOf() method is used to specify the search index, and because it uses the reverse method to retrieve:
When its value is greater than or equal to the length of the array, the entire array will be searched.
When the value is a negative number, the index position is equal to the length of the array plus the given negative number. If the 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 method provided by JavaScript to achieve this.
##The similarities between join() and toString() methods:
The difference between join() and toString() methods Point:
Note that the
The above is the detailed content of Common JavaScript array methods and teach you how to transpose a matrix. For more information, please follow other related articles on the PHP Chinese website!