简体   繁体   English

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

[英]How to create a multi dimensional object or array in javascript or jquery

I want to store the data in to multidimensional arrays or object and send the data to the controller. 我想将数据存储到多维数组或对象中,然后将数据发送到控制器。 I have defined the object in javascript like 我已经在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;

If the see the json content then it is displaying only the latest information. 如果看到json内容,则它仅显示最新信息。 but it is not showing the content that is saved earlier. 但它不会显示先前保存的内容。 how can i save the content. 我如何保存内容。

The problem is that you are overriding: dynamicprofile.profilename, dynamicprofile.idarray and dynamicprofile.trueorfalse. 问题是您要覆盖:dynamicprofile.profilename,dynamicprofile.idarray和dynamicprofile.trueorfalse。

Maybe you can try one of these solutions among others: 也许您可以尝试以下解决方案之一:

1. 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. 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
 });

u can do it in following way 你可以按照以下方式做

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM