There is no concept of two-dimensional arrays in JavaScript __java

Source: Internet
Author: User

It is sometimes necessary to manipulate arrays when writing webclinet-side scripts, and the syntax for declaring arrays in JavaScript is

Dim variable = new Array ();

It should be noted that there is no two-dimensional array concept in JavaScript, only one-dimensional.

Cases:

One-dimensional array generation, assignment, display method
function GetArray ()
{
var arr = new Array ();
Arr[0] = "0";
ARR[1] = "1";

for (i=0;i<arr.length;i++)
{
Alert (Arr[i]);
}
}

Two-dimensional array generation, assignment, display method (this method error)
function Get2darray ()
{
Alert (' Such declaration, assignment, display of two-dimensional array method is illegal ');
var arr = new Array ();
int[][] arr = new Array (); This writing is not true at all, and does not conform to JavaScript syntax, which can cause JavaScript scripts throughout the page to not execute
Arr[0][0] = 1;
ARR[0][1] = 2;
Arr[1][0] = 3;
ARR[1][1] = 4;

for (i=0; i<arr.length;i++)
{
for (j=0; j<arr[i].length;j++)
{
Alert (Arr[i][j]);
}
}
}

This is a common way of declaring arrays (in other languages, especially those that are commented out), but it turns out that this is illegal, further explaining that there is no definition of two-dimensional arrays in JavaScript.

Think of an old saying "only unexpectedly, not to do, there are solutions."

A workaround that copies each element of a one-dimensional array, and its value is also a one-dimensional array.

Two-dimensional array generation, assignment, display method (workaround)
function Get2darray2 ()
{
var arr = new Array ();
Arr[0] = new Array ();
ARR[1] = new Array ();

Arr[0][0] = "00";
arr[0][1]= "01";
arr[1][0]= "10";
arr[1][1]= "11";

for (i=0; i<arr.length;i++)
{
for (j=0; j<arr[i].length;j++)
{
Alert (Arr[i][j]);
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.