2013-10-24 122 views
0

我想將數據存儲到多維數組或對象中並將數據發送到控制器。我已經定義在JavaScript中的物體,像如何在javascript或jquery中創建多維對象或數組

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內容則僅顯示最新信息。但它沒有顯示先前保存的內容。我如何保存內容。 dynamicprofile.profilename,dynamicprofile.idarray和dynamicprofile.trueorfalse:

回答

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.

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 
}); 
+0

謝謝你,我得到了我已經使用第一個 – Jonathan

0

U可以在下列方式

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

呀我已經找到另一種方式。我接受了另一個對象'var objname = {};'和第一個obj'objname [「cert」]'和第二個對象'objname [「projects」]''。謝謝你的回答 – Jonathan

相關問題