Tips for creating two-dimensional arrays in JavaScript

Tips for creating two-dimensional arrays in JavaScript

Creation of a two-dimensional array in Js:

First of all, JavaScript only supports one-dimensional arrays, but we can implement matrices and multidimensional arrays through some methods.

There are no problems with the normal creation method:

(1) Create a two-dimensional array by nesting a one-dimensional array:

let arr = [] ;
a[0] = [1,2,3,4,5,6];
a[1] = [10,20,30,40,50,60]

Then use a double for loop to iterate over the elements in this two-dimensional array

So using this method to create a multidimensional array, no matter how many dimensions there are, you can traverse it through nested loops

Methods for encountering problems:

  let arr1 = new Array(10).fill(new Array(10).fill(0))

The console prints arr1 :

Please add a description of the image

At this time, if you want to set arr[0][0] = 1 , you will find that the first item of all sub-arrays of the two-dimensional array is changed to 1.

Please add a description of the image

reason:

Please add a description of the image

In summary, it is better to choose an honest creation method:

var a = new Array();

for(var i=0;i<5;i++){ //The length of one dimension is 5

    a[i] = new Array();

    for(var j=0;j<5;j++){ //The length of the two-dimensional is 5

    	a[i][j] = 0;
   }

}

This is the end of this article about the creation techniques of two-dimensional arrays in JavaScript. For more relevant JavaScript two-dimensional array content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JS implementation example of converting a two-dimensional array into a json format string
  • JS implements a simple example of permutation and combination operations of two-dimensional array elements
  • Example of how to dynamically create a two-dimensional array in JavaScript
  • Analysis of deep and shallow copies of javascript two-dimensional arrays and objects
  • Python two-dimensional key-value array generation and conversion to json example
  • About the conversion between one-dimensional array and two-dimensional array in JS
  • JavaScript array operations: rotating two-dimensional arrays

<<:  MySQL efficient query left join and group by (plus index)

>>:  How to submit a pure HTML page, pass parameters, and verify identity

Recommend

Method of using MySQL system database for performance load diagnosis

A master once said that you should know the datab...

Detailed example of using useState in react

useState useState adds some internal state to a c...

How to view the network routing table in Ubuntu

What are Routing and Routing Table in Linux? The ...

LinkedIn revamps to simplify website browsing

Business social networking site LinkedIn recently...

Pagination Examples and Good Practices

<br />Structure and hierarchy reduce complex...

WeChat applet component development: Visual movie seat selection function

Table of contents 1. Introduction 1. Component da...

Sharing some details about MySQL indexes

A few days ago, a colleague asked me a question a...

Detailed explanation of the wonderful CSS attribute MASK

This article will introduce a very interesting at...

Analysis of GTK treeview principle and usage

The GtkTreeView component is an advanced componen...

Causes and solutions for MySQL deadlock

The database, like the operating system, is a sha...

Example code of layim integrating right-click menu in JavaScript

Table of contents 1. Effect Demonstration 2. Impl...

Detailed examples of float usage in HTML/CSS

1. Basic usage examples of float 1. Let's fir...

JS removeAttribute() method to delete an attribute of an element

In JavaScript, use the removeAttribute() method o...