In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "JavaScript how to transpose matrix". In daily operation, I believe many people have doubts about how to transpose matrix in JavaScript. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to transpose matrix in JavaScript". Next, please follow the editor to study!
I. creation and traversal of common two-dimensional array operations
In the previous chapter, we have learned various ways to create an one-dimensional array, and after understanding how to create an one-dimensional array, the creation of a two-dimensional array is very simple, just set the array elements to the array.
After creating a two-dimensional array, how do you traverse the elements in the two-dimensional array and manipulate them?
One-dimensional array can make use of for, for... In or for... Of (provided by ES6) for traversal.
The two-dimensional array only needs to traverse the elements of the array again after traversing the array.
In addition, in Web project development, multidimensional arrays are often created by adding elements to multidimensional empty arrays. Let's take 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 make sure that the added element (such as arr [I]) has been created as an array, otherwise the program will report "Uncaught TypeError..." Mistake.
Be careful
When creating multidimensional arrays, although JavaScript does not limit the dimension of the array, in practical application, it is recommended to use three-dimensional or less arrays to save data in order to facilitate code reading, debugging and maintenance.
[case] two-dimensional array transposition
The transpose of a two-dimensional array refers to saving the horizontal elements of a two-dimensional array as vertical elements.
Code implementation ideas:
Find the rule: res [0] [0] = arr [0] [0], res [0] [1] = arr [1] [0], res [0] [2] = arr [2] [0].
Conclusion: res [I] [j] = arr [j] [I]. two
Res array length = the length of the arr element, such as arr [0]. three
The length of the res element, such as res [0], = the length of the arr array. four
Complete the creation and traversal of res according to ③ and ④, and transpose by ②.
In order to give you a sense of achievement, I will not post the code. If you have any questions, you can ask them in the comments section. In fact, the matrix can be stored in the array, after the matrix transpose directly run the code on the line.
Common array method stack and queue method
In JavaScript, in addition to the ways to add and remove array elements described earlier, you can also use the methods provided by the Array object to simulate stack and queue operations.
Adds a new element of the array at the end or beginning of the array.
Deletes an array element at the end or beginning of the array.
The return values of the push () and unshift () methods are the length of the new array.
The pop () and shift () methods return the removed array elements.
Retrieval method
In development, to detect whether a given value is an array, or to find the location of a specified element in an array.
Except for the Array.isArray () method, the other methods in the table are retrieved by default from the location of the specified array index, and in the same way as the operator "= =", that is, more successful results are returned only if they are congruent.
Includes () and Array.isArray () methods
The first parameter of the includes () method represents the value to be found.
The second parameter of the includes () method is used to specify the subscript to look up in the array.
When set to greater than the length of the array, the array is not retrieved and is directly returned to false.
When set to a number less than 0, the index position retrieved is equal to the length of the array plus the specified negative number, and if the result is still less than 0, the entire array is retrieved.
IndexOf () method
IndexOf () is used to retrieve the first given value in the array from the specified subscript position. If it exists, it returns the corresponding element subscript, otherwise it returns-1.
Be careful
The second parameter of the indexOf () method specifies the subscript to start the lookup:
Returns-1 directly when its value is greater than or equal to the length of the array.
When the value is negative, the subscript position is equal to the length of the array plus the specified negative number, and if the result is still less than 0, the entire array is retrieved.
LastIndexOf () method
The lastIndexOf () method provided by the Array object for the subscript of the last given value retrieved from the specified subscript location in the array. Unlike the indexOf () retrieval method, the lastIndexOf () method defaults to reverse retrieval, that is, from the end of the array to the beginning of the array.
Be careful
The second parameter of the lastIndexOf () method is used to specify the subscript for the lookup, and because it is retrieved in reverse:
When its value is greater than or equal to the length of the array, the entire array is found.
When its value is negative, the index position is equal to the length of the array plus the given negative number, and if its value is still negative, it directly returns-1.
Array to string
If you need to convert an array to a string in development, you can use the method provided by JavaScript.
The similarities between the join () and toString () methods:
You can convert a multidimensional array to a string, using comma concatenation by default.
When the array element is undefined, null, or empty array, the corresponding element is converted to an empty string
The difference between the join () and toString () methods:
The join () method specifies the symbol for concatenating array elements.
Other methods
In addition to the several common methods explained earlier, JavaScript provides a number of other commonly used array methods. For example, merge arrays, array shallow copies, reverse the order of array elements, and so on.
Be careful
The slice () and concat () methods return a new array after execution, which does not affect the original array, while the rest of the methods affect the original array after execution.
When the value of the first parameter of the splice () method is equal to or greater than the length of the array, the operation starts at the end of the array; when the value is negative, the subscript position is equal to the length of the array plus the specified negative number, and if the value is still negative, the operation starts at the beginning of the array.
At this point, the study of "how to transpose the matrix of JavaScript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.