如何在jquery或js中创建动态二维数组[重复]

时间:2021-05-20 12:53:33

This question already has an answer here:

这个问题在这里已有答案:

I need to create global two dimensional array in jquery or javascript

我需要在jquery或javascript中创建全局二维数组

My function is like this

我的功能是这样的

<script>

var globalArray[0] = new Array();

function createArray(){

    alert(globalArray[0]);         
}

</script>

<div><input type='button' value='save' onclick='createArray();'> </div>

On click of that button I am getting this error "globalArray[0] is undefined"

点击该按钮我收到此错误“globalArray [0]未定义”

How can I create global dynamic multi dimensional array.

如何创建全局动态多维数组。

2 个解决方案

#1


7  

if (!globalArray[index]) 
    globalArray[index] = []; // init the array.

globalArray[index].push(name);

You have a typo with the dot:

你有一个点的拼写错误:

$.("#uname").val(); 

Change to:

改成:

$("#uname").val();

What are you trying to do with this code?

你想用这个代码做什么?


Update: (The question was totally edited.)

更新:(问题已完全编辑。)

Your code:

你的代码:

var globalArray[0] = new Array(); 

globalArray[0] is invalid variable name, you need first to declare the array:

globalArray [0]是无效的变量名,需要先声明数组:

var globalArray = []; // Array literal.
globalArray[0] =  [] // The element at position 0 is new an array. 

#2


1  

Intead of

Intead of

if(loop == 0){
 globalArray[index][0] = uname;
}else{
  globalArray[index][loop++] = uname;
}

Use this

用这个

if(loop > 0){
    globalArray[index][loop++] = uname;     
}else{
    globalArray[index][0] = uname;      
}

#1


7  

if (!globalArray[index]) 
    globalArray[index] = []; // init the array.

globalArray[index].push(name);

You have a typo with the dot:

你有一个点的拼写错误:

$.("#uname").val(); 

Change to:

改成:

$("#uname").val();

What are you trying to do with this code?

你想用这个代码做什么?


Update: (The question was totally edited.)

更新:(问题已完全编辑。)

Your code:

你的代码:

var globalArray[0] = new Array(); 

globalArray[0] is invalid variable name, you need first to declare the array:

globalArray [0]是无效的变量名,需要先声明数组:

var globalArray = []; // Array literal.
globalArray[0] =  [] // The element at position 0 is new an array. 

#2


1  

Intead of

Intead of

if(loop == 0){
 globalArray[index][0] = uname;
}else{
  globalArray[index][loop++] = uname;
}

Use this

用这个

if(loop > 0){
    globalArray[index][loop++] = uname;     
}else{
    globalArray[index][0] = uname;      
}