Javascript 根据搜索键格式化.serializeArray()

Javascript 根据搜索键格式化.serializeArray(),javascript,jquery,json,Javascript,Jquery,Json,这是我的代码用于序列化表单的当前代码 $.fn.serializeObject = function () { var o = {}; var a = this.serializeArray(); console.log("A:"+a); $.each(a, function () { if (o[this.name] !== undefined) { if (!o[this.name].push) {

这是我的代码用于序列化表单的当前代码

 $.fn.serializeObject = function () {
     var o = {};
     var a = this.serializeArray();
     console.log("A:"+a);
     $.each(a, function () {
         if (o[this.name] !== undefined) {
             if (!o[this.name].push) {
                 o[this.name] = [o[this.name]];
             }
             o[this.name].push(this.value || '');
         } else {
             o[this.name] = this.value || '';
         }
     });
     return o;
 };
所以它会给出一个类似的输出

{
  "acctType1": "individual",
  "compare_act1": "contains",
  "match_name_act1": "accountName",
  "text_act1": "",
  "acctType2": "individual",
  "compare_act2": "contains",
  "match_name_act2": "accountName",
  "text_act2": "",
  "transType1": "401kContribution",
  "compare_trans1": "contains",
  "match_name_trans1": "description",
  "text_trans1": "",
  "transType2": "401kContribution",
  "compare_trans2": "contains",
  "match_name_trans2": "description",
  "text_trans2": ""
}
但是,我想这样做:

{
  "acctobj": {
    "acctType1": "individual",
    "compare_act1": "contains",
    "match_name_act1": "accountName",
    "text_act1": "",
    "acctType2": "individual",
    "compare_act2": "contains",
    "match_name_act2": "accountName",
    "text_act2": ""
  },
  "transobj": {
    "transType1": "401kContribution",
    "compare_trans1": "contains",
    "match_name_trans1": "description",
    "text_trans1": "",
    "transType2": "401kContribution",
    "compare_trans2": "contains",
    "match_name_trans2": "description",
    "text_trans2": ""
  }
}
所以基本上我必须进入acct{},直到搜索遇到trans,然后将它推入trans

所以搜索变量a并继续按acctobj,直到找到trans,然后按transobj

然后在var o={}中同时推送acctobj和transobj


提前谢谢

你的问题是什么?我想修改上面的json。所以我试图修改serializeObject,但不起作用
 $.fn.serializeObject = function () {
     var o = {};
     var acctobj={};
     var transobj={};
     var a = this.serializeArray();
     $.each(a, function () {
         if (acctobj[this.name.search("trans")] =-1 ) {
             if (!acctobj[this.name].push) {
                 acctobj[this.name] = [acctobj[this.name]];
             }
             acctobj[this.name].push(this.value || '');
         } else {
             transobj[this.name] = this.value || '';
         }
     });

     return o;
 };