Home >Web Front-end >JS Tutorial >Common JavaScript array methods and teach you how to transpose a matrix

Common JavaScript array methods and teach you how to transpose a matrix

WBOY
WBOYforward
2022-03-28 12:00:033301browse

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.

Common JavaScript array methods and teach you how to transpose a matrix

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.

Common JavaScript array methods and teach you how to transpose a matrix

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.

Common JavaScript array methods and teach you how to transpose a matrix

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.

Common JavaScript array methods and teach you how to transpose a matrix

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.

Common JavaScript array methods and teach you how to transpose a matrix

  • 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.

Common JavaScript array methods and teach you how to transpose a matrix

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

Common JavaScript array methods and teach you how to transpose a matrix

  • 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.

Common JavaScript array methods and teach you how to transpose a matrix

Common JavaScript array methods and teach you how to transpose a matrix

##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.
Other methods

In addition to the several common methods explained previously, JavaScript also provides many other commonly used array methods. For example, merge arrays, shallow copy arrays, reverse the order of array elements, etc.

Common JavaScript array methods and teach you how to transpose a matrix

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.
Related recommendations:

javascript tutorial

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete