最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dynamic - Javascript 2D array - Stack Overflow

programmeradmin0浏览0评论

I am trying to use 2 dimensional array in javascript for storing strings. But I am not able to get the values correctly. Below is my code.

      var mentstore=new Array();
        function creating(id,day)
        {
            if(mentstore[day,id] != null)
         {
             alert("It already exists: mentstore["+day+"]["+id+"]"+mentstore[day,id] );
             var textinput="<div id='closeit'>Comments:<input type='text' name='m["+day+"]  ["+id+"]' value='"+mentstore[day,id]+"'/></div>
                               <div id='closing' onclick='closement("+id+","+day+")'>:)</div>";
         }
            else
            {
                var textinput="<div id='closeit'>Comments:<input type='text' name='m["+day+"]  ["+id+"]' /></div>
                               <div id='closing' onclick='closement("+id+","+day+")'>:)</div>";  
                $('#m').html(textinput);
            }

    function closement(id,day)
    {
        m.style.visibility='hidden';
        var str='m['+day+']['+id+']';
        var element = document.getElementById(str);
     if(element.value !=null)
     {
      mentstore[day,id]=element.value;
      alert('New values stored: mentstore['+day+']['+id+']'+mentstore[day,id]);
     }
    }

So in the above code if mentstore[0,0]='man' then mentstore[1,0] and [2,0] and [3,0] ....[7,0] are also filled with 'man'. Same thing is happening with mentstore[0,1] even mentstore[4,1] scenarios. Can any one please provide any tutorial or sample code where we can dynamically create javascript 2d arrays. Thanks in advance.

I am trying to use 2 dimensional array in javascript for storing strings. But I am not able to get the values correctly. Below is my code.

      var mentstore=new Array();
        function creating(id,day)
        {
            if(mentstore[day,id] != null)
         {
             alert("It already exists: mentstore["+day+"]["+id+"]"+mentstore[day,id] );
             var textinput="<div id='closeit'>Comments:<input type='text' name='m["+day+"]  ["+id+"]' value='"+mentstore[day,id]+"'/></div>
                               <div id='closing' onclick='closement("+id+","+day+")'>:)</div>";
         }
            else
            {
                var textinput="<div id='closeit'>Comments:<input type='text' name='m["+day+"]  ["+id+"]' /></div>
                               <div id='closing' onclick='closement("+id+","+day+")'>:)</div>";  
                $('#m').html(textinput);
            }

    function closement(id,day)
    {
        m.style.visibility='hidden';
        var str='m['+day+']['+id+']';
        var element = document.getElementById(str);
     if(element.value !=null)
     {
      mentstore[day,id]=element.value;
      alert('New values stored: mentstore['+day+']['+id+']'+mentstore[day,id]);
     }
    }

So in the above code if mentstore[0,0]='man' then mentstore[1,0] and [2,0] and [3,0] ....[7,0] are also filled with 'man'. Same thing is happening with mentstore[0,1] even mentstore[4,1] scenarios. Can any one please provide any tutorial or sample code where we can dynamically create javascript 2d arrays. Thanks in advance.

Share Improve this question edited Dec 28, 2009 at 15:34 Enjoy coding asked Dec 28, 2009 at 15:16 Enjoy codingEnjoy coding 4,36612 gold badges43 silver badges50 bronze badges 2
  • [day,id] is equivalent to just doing [id] – Eli Grey Commented Dec 28, 2009 at 15:45
  • Thank you all for helping. It is now working fine. I need to verify it more. Thanks again.. – Enjoy coding Commented Dec 28, 2009 at 15:58
Add a ment  | 

3 Answers 3

Reset to default 6

Replace mentstore[day,id] with mentstore[day][id]. That's the syntax for multi-dimensional arrays.

Use mentstore[0][0] instead of mentstore[0,0]. Also, use strict paraison whenever loose paraison is not needed:

var mentstore = [];

function creating(id,day)
{
    if(mentstore[day] === undefined) mentstore[day] = [];
    if(mentstore[day][id] !== undefined)
    {
        alert("It already exists: mentstore["+day+"]["+id+"]"+mentstore[day][id] );
        var textinput="<div id='closeit'>Comments:<input type='text' name='m["+day+"]  ["+id+"]' value='"+mentstore[day][id]+"'/></div>
                       <div id='closing' onclick='closement("+id+","+day+")'>:)</div>";
    }
    else
    {
        var textinput="<div id='closeit'>Comments:<input type='text' name='m["+day+"]  ["+id+"]' /></div>
                       <div id='closing' onclick='closement("+id+","+day+")'>:)</div>";  
        $('#m').html(textinput);
    }

function closement(id,day)
{
    m.style.visibility='hidden';
    var element = document.getElementById(str);
    if(element.value !== '')
    {
        mentstore[day][id]=element.value;
        alert('New values stored: mentstore['+day+']['+id+']'+mentstore[day][id]);
    }
}

edit: in the original code, str is undefined and the execution fails. You can fix it in closement 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:

mentstore[day+','+id] = ...

i.e. use a string with to ponents as the key.

发布评论

评论列表(0)

  1. 暂无评论