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
Creation and Traversal
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?
- One-dimensional arrays can be traversed using for, for...in or for...of (provided by ES6).
- For two-dimensional arrays, you only need to traverse the elements of the array again after traversing the array.
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:
- Find the pattern: 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]. ②
- res array length = the length of arr element (such as arr[0]). ③
- The length of the res element (such as res[0]) = the length of the arr array. ④
- Follow ③ and ④ to complete the creation and traversal of res, and press ② to transpose.
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
Stack and queue 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.
- Add a new element of the array at the end or beginning of the array.
- Delete array elements at the end or beginning of the array.
- The return value of the push() and unshift() methods is the length of the new array.
- The pop() and shift() methods return the removed array elements.
Retrieval method
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
- 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 be searched in the array.
- When set to greater than the length of the array, the array will not be retrieved and false will be returned directly.
- 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. If the result is still a number less than 0, the entire array is retrieved.
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:
- When its value is greater than or equal to the length of the array, -1 is returned directly.
- When the value is a negative number, the search subscript position is equal to the length of the array plus the specified negative number. If the result is still a number less than 0, the entire array is retrieved.
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.
Convert array to string
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:
- Multidimensional arrays can be converted into strings. By default, commas are used for concatenation.
- When the array element is undefined, null or an empty array, the corresponding element will be converted to an empty string
The difference between join() and toString() methods Point:
- #join() method can specify the symbol to connect array elements.
Note that the
- slice() and concat() methods return a new array after execution. It affects the original array, and the remaining methods will affect the original array after execution.
- When the value of the first parameter of the splice() method is equal to or greater than the array length, the operation starts from the end of the array; when the value is a negative number, the subscript position is equal to the array length plus the specified negative number. If the value is still negative, the operation starts from the beginning of the array.
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!

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor