[英]Javascript 2D array
I am trying to use 2 dimensional array in javascript for storing strings. 我正在尝试在JavaScript中使用二维数组来存储字符串。 But I am not able to get the values correctly.
但是我无法正确获取值。 Below is my code.
下面是我的代码。
var commentstore=new Array();
function creating(id,day)
{
if(commentstore[day,id] != null)
{
alert("It already exists: commentstore["+day+"]["+id+"]"+commentstore[day,id] );
var textinput="<div id='closeit'>Comments:<input type='text' name='comm["+day+"] ["+id+"]' value='"+commentstore[day,id]+"'/></div>
<div id='closing' onclick='closecomment("+id+","+day+")'>:)</div>";
}
else
{
var textinput="<div id='closeit'>Comments:<input type='text' name='comm["+day+"] ["+id+"]' /></div>
<div id='closing' onclick='closecomment("+id+","+day+")'>:)</div>";
$('#comm').html(textinput);
}
function closecomment(id,day)
{
comm.style.visibility='hidden';
var str='comm['+day+']['+id+']';
var element = document.getElementById(str);
if(element.value !=null)
{
commentstore[day,id]=element.value;
alert('New values stored: commentstore['+day+']['+id+']'+commentstore[day,id]);
}
}
So in the above code if commentstore[0,0]='man' then commentstore[1,0] and [2,0] and [3,0] ....[7,0] are also filled with 'man'. 因此,在上面的代码中,如果commentstore [0,0] ='man',则commentstore [1,0]和[2,0]和[3,0] .... [7,0]也都填充有'man '。 Same thing is happening with commentstore[0,1] even commentstore[4,1] scenarios.
评论库[0,1]甚至评论库[4,1]场景都在发生同样的事情。 Can any one please provide any tutorial or sample code where we can dynamically create javascript 2d arrays.
任何人都可以提供任何教程或示例代码,以便我们动态创建javascript 2d数组。 Thanks in advance.
提前致谢。
Replace commentstore[day,id]
with commentstore[day][id]
. 用
commentstore[day][id]
替换commentstore[day,id]
commentstore[day][id]
。 That's the syntax for multi-dimensional arrays. 这就是多维数组的语法。
Use commentstore[0][0] instead of commentstore[0,0]. 使用commentstore [0] [0]代替commentstore [0,0]。 Also, use strict comparaison whenever loose comparaison is not needed:
另外,在不需要松散比较的情况下,请使用严格的比较:
var commentstore = [];
function creating(id,day)
{
if(commentstore[day] === undefined) commentstore[day] = [];
if(commentstore[day][id] !== undefined)
{
alert("It already exists: commentstore["+day+"]["+id+"]"+commentstore[day][id] );
var textinput="<div id='closeit'>Comments:<input type='text' name='comm["+day+"] ["+id+"]' value='"+commentstore[day][id]+"'/></div>
<div id='closing' onclick='closecomment("+id+","+day+")'>:)</div>";
}
else
{
var textinput="<div id='closeit'>Comments:<input type='text' name='comm["+day+"] ["+id+"]' /></div>
<div id='closing' onclick='closecomment("+id+","+day+")'>:)</div>";
$('#comm').html(textinput);
}
function closecomment(id,day)
{
comm.style.visibility='hidden';
var element = document.getElementById(str);
if(element.value !== '')
{
commentstore[day][id]=element.value;
alert('New values stored: commentstore['+day+']['+id+']'+commentstore[day][id]);
}
}
edit: in the original code, str is undefined and the execution fails. 编辑:在原始代码中, str未定义,执行失败。 You can fix it in closecomment with:
您可以通过以下方式对此进行修正:
var element = $('#closeit > input').eq(0);
JavaScript only supports arrays with a single index but the index can be anything, so if you really want two dimensional arrays, do this: JavaScript仅支持具有单个索引的数组,但索引可以是任何值,因此,如果您确实想要二维数组,请执行以下操作:
commentstore[day+','+id] = ...
ie use a string with to components as the key. 即,使用带有to成分的字符串作为键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.