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

Detailed explanation of the background-position percentage principle

When I was helping someone adjust the code today,...

Detailed steps for configuring Tomcat server in IDEA 2020

The steps for configuring Tomcat in IDEA 2020 are...

The top fixed div can be set to a semi-transparent effect

Copy code The code is as follows: <!DOCTYPE ht...

JavaScript method to detect the type of file

Table of contents 1. How to view the binary data ...

JavaScript to achieve magnifying glass effect

This article shares the specific code for JavaScr...

How to use tcpdump to capture packets in Linux system

Let me look at the example code first: 1. Common ...

Detailed explanation of Docker common commands Study03

Table of contents 1. Help Command 2. Mirror comma...

Summary of important mysql log files

Author: Ding Yi Source: https://chengxuzhixin.com...

Why does MySQL paging become slower and slower when using limit?

Table of contents 1. Test experiment 2. Performan...

Brief analysis of the introduction and basic usage of Promise

Promise is a new solution for asynchronous progra...

Implementation of Docker packaging image and configuration modification

I have encountered many problems in learning Dock...

Detailed tutorial on installing VirtualBox and Ubuntu 16.04 under Windows system

1. Software Introduction VirtualBox VirtualBox is...

What are the advantages of using B+ tree index in MySQL?

Before understanding this problem, let's firs...