使用JQuery foreach创建一个多维数组

时间:2015-03-02 02:35:10

标签: jquery multidimensional-array

我想将jqgrid与嵌套子网格一起使用。我通过内容类型为JSON的Ajax调用从数据库中提取数据。 这就是我的JSON结构的外观。我需要从中形成一个数组对象。

records : 

{

contracts[
           {
              contractid: 1,
              contractname: contractname1,
              status: "open",

              contractLines: [                --contractLine1
                 {
                 contractLineid: 11,
                 item name: "contractLine1",
                 description : "abcd",
                  products: [
                   {
                   productid : 21,
                   productname : "product21"
                   },

                   {
                   productid : 22,
                   productname : "product22"
                   }
                   ]
                 },

                 {
                 contractLineid: 12,            contractLine2
                 item name: "contractLine1",
                 description : "abcd",
                  products: [
                   {
                   productid : 23
                   productname : "product23"
                     },      

                   {
                   productid : 24,
                   productname : "product24"
                   } 
                   ]
                 }

              ]

          },

          {
              contractid: 2,
              contractname: contractname2,
              status: "open",

              contractLines: [                --contractLine1
                 {
                 contractLineid: 2,
                 item name: "contractLine21",
                 description : "abcd",
                  products: [
                   {
                   productid : 31,
                   productname : "product31"
                   },

                   {
                   productid : 32,
                   productname : "product32"
                   }
                   ]
                 },

                 {
                 contractLineid: 22,           -- contractLine2
                 item name: "contractLine22",
                 description : "abcd",
                  products: [
                   {
                   productid : 33
                   productname : "product33"
                     },      

                   {
                   productid : 34,
                   productname : "product34"
                   } 
                   ]
                 }

              ]

          }

       ]

}-- records

我将3层数据绑定到网格列,如下所示:

            var records = [];
            var records1 = [];
            var records2 = [];
            //============================================================
            // build records of Contracts
            //============================================================
            $.each(contracts, function (index, contract) {
                records[index] = {};
                records[index]['contractid'] = contract.ID;
                records[index]['description'] = contract.Description;
                records[index]['status'] = contract.Status.Value;
                records[index]['classification'] = contract.Classification;
                records[index]['startdate'] = PD.Utilities.FormatDateDDMMMYYYY(contract.DateStart);
                records[index]['enddate'] = PD.Utilities.FormatDateDDMMMYYYY(contract.DateEnd);
                records[index]['canceldate'] = PD.Utilities.FormatDateDDMMMYYYY(contract.DateCancel);


                //============================================================
                // build records of Contract Lines
                //============================================================
                $.each(contract.ContractLines, function (index2, contractLine) {

                    records1[index2] = {};
                    records1[index2]['contractlineid'] = contractLine.ID;
                    records1[index2]['itemname'] = contractLine.ItemName;
                    records1[index2]['startdate'] = PD.Utilities.FormatDateDDMMMYYYY(contractLine.DateStart);
                    records1[index2]['enddate'] = PD.Utilities.FormatDateDDMMMYYYY(contractLine.DateEnd);
                    records1[index2]['canceldate'] = PD.Utilities.FormatDateDDMMMYYYY(contractLine.DateCancel);
                    records1[index2]['vendorname'] = contractLine.Vendor.Name;
                    records1[index2]['description'] = contractLine.Description;
                    records1[index2]['isshipped'] = contractLine.IsShipped;


                    //============================================================
                    // build records of Products
                    //============================================================
                    $.each(contractLine.Products, function (index3, product) {

                        records2[index3] = {};
                        records2[index3]['productid'] = product.ID;
                        records2[index3]['name'] = product.Name;
                        records2[index3]['description'] = product.Description;

                    });



                });



            });

1 个答案:

答案 0 :(得分:0)

来自$.getJSON

$.getJSON( "ajax/test.json", function( data ) {
  var items = [];
  $.each( data, function( key, val ) {
    items.push( "<li id='" + key + "'>" + val + "</li>" );
  });

  $( "<ul/>", {
    "class": "my-new-list",
    html: items.join( "" )
  }).appendTo( "body" );
});