如何在javascript或jquery中创建多维对象或数组

时间:2013-10-24 15:19:47

标签: javascript jquery

我想将数据存储到多维数组或对象中,并将数据发送到控制器。我已经在javascript中定义了对象

    var dynamicprofile = new Object();


        var certidarry = [];
        var trueorfalse = [];
        dynamicprofile.profilename = "certifications";
        $(".certclass").each(function (index, element) {
            certidarry.push(i);

            if (element.checked == false) {
                trueorfalse.push(false);                   
            }
            else if (element.checked == true) {
                trueorfalse.push(true);
            }
        });
        dynamicprofile.idarray = certidarry;
        dynamicprofile.trueorfalse = trueorfalse;


        dynamicprofile.profilename = "projects";
        var projectidarry12 = [];
        var trueorfalsearry12 = [];
        $(".Projectsclass").each(function (index, element) { 
            projectidarry12.push(index);               
            if (element.checked == false) {
                trueorfalsearry12.push(false);
            }
            else if (element.checked == true) {
                trueorfalsearry12.push(true);
            }
        });
        dynamicprofile.idarray = projectidarry12;
        dynamicprofile.trueorfalse = trueorfalsearry12;

如果看到json内容,那么它只显示最新信息。但它没有显示先前保存的内容。我该如何保存内容。

2 个答案:

答案 0 :(得分:1)

问题是你要覆盖:dynamicprofile.profilename,dynamicprofile.idarray和dynamicprofile.trueorfalse。

也许您可以尝试其中一种解决方案:

1

 var dynamicprofile = new Object();

    var certidarry = [];
    var trueorfalse = [];
    dynamicprofile['certifications'] = {};
    $(".certclass").each(function (index, element) {
        certidarry.push(index);

        if (element.checked == false) {
            trueorfalse.push(false);                   
        }
        else if (element.checked == true) {
            trueorfalse.push(true);
        }
    });
    dynamicprofile['certifications'].idarray = certidarry;
    dynamicprofile['certifications'].trueorfalse = trueorfalse;

    dynamicprofile['projects'] = {};
    var projectidarry12 = [];
    var trueorfalsearry12 = [];
    $(".Projectsclass").each(function (index, element) { 
        projectidarry12.push(index);               
        if (element.checked == false) {
            trueorfalsearry12.push(false);
        }
        else if (element.checked == true) {
            trueorfalsearry12.push(true);
        }
    });
    dynamicprofile['projects'].idarray = projectidarry12;
    dynamicprofile['projects'].trueorfalse = trueorfalsearry12;

-

2

 var dynamicprofile = [];

 var certidarry = [];
 var trueorfalse = [];

 $(".certclass").each(function (index, element) {
      certidarry.push(index);

      if (element.checked == false) {
          trueorfalse.push(false);                   
      }
      else if (element.checked == true) {
          trueorfalse.push(true);
      }
 });
 dynamicprofile.push({ 
  profilename: "certifications",
  idarray: certidarry,
  trueorfalse: trueorfalse
 });

 var projectidarry12 = [];
 var trueorfalsearry12 = [];
 $(".Projectsclass").each(function (index, element) { 
     projectidarry12.push(index);               
     if (element.checked == false) {
         trueorfalsearry12.push(false);
     }
     else if (element.checked == true) {
         trueorfalsearry12.push(true);
     }
 });
 dynamicprofile.push({ 
  profilename: "projects",
  idarray: projectidarry12,
  trueorfalse: trueorfalsearry12
 });

答案 1 :(得分:0)

你可以用以下方式做到这一点

 var a=new Array();
    for(vr i=0;i<10;i++)
    {
     a[i]=new Array();
    a[i][0]='value1';
    a[i][1]='value2';
    }