在数组对象中创建数组对象

时间:2013-09-18 07:41:32

标签: javascript jquery javascript-objects

我有一个数组对象需要添加另一个数组对象。所以我有一个对象的细节,需要将表中的行作为数组添加到该对象。我已经尝试了一些关于stackoverflow的建议,但似乎都没有工作,我不确定这与表是由js创建的事实有关。

// Adding Cosignment number
$('#parcel-overview').append(
 // Add main tables which will display the added packages
 '<table border="1">' +
 '<thead><tr><th>Packages</th><th>Weight</th> <th>Vol Weight</th><th>Charge Weight</th> <th>Price</th></tr></thead><tbody id="parcels-added-overview"></tbody> ' +
 '</table>'
 );

for (var i = 0; i < packageNum; i++) {

    var ii = (i + 1).toString();

    // Working out volemetric weight
    wei = $('#added-parcels #weighting-' + ii + ' input').val();
    len = $('#added-parcels #lengthing-' + ii + ' input').val();
    wid = $('#added-parcels #widthing-' + ii + ' input').val();
    hei = $('#added-parcels #heighting-' + ii + ' input').val();

    //Calculating Volumetric weight
    tot = ((len * wid * hei) / 5000).toFixed(1);

    pri = (tot * 23).toFixed(2);

    chr = (tot * 12).toFixed(2);
    $('#parcels-added-overview').append(
   '<tr>' +
     '<td class="par-id">' + (i + 1).toString() + '</td>' +
     '<td class="par-weight">' + wei.toString() + ' kg\'s</td>' +
     '<td class="par-vol-weight">' + tot.toString() + ' kg\'s</td>' +
     '<td class="par-charge-weight">R ' + chr.toString() + '</td>' +
     '<td class="par-price">R ' + pri.toString() + ' </td>' +
   '</tr>'
    );
}

然后,我想将已动态添加的表的值添加到对象数组中,然后将其添加到主对象数组中。

 var parcelObj = new Object();

    $.each($('#parcels-added-overview tr'),function (index) {

        parcelObj.parcelId = $(this).children('.par-id').text();
        parcelObj.totalWeight = $(this).children('.par-weight').text();
        parcelObj.volWeight = $(this).children('.par-vol-weight').text();
        parcelObj.chargeWeight = $(this).children('.par-charge-weight').text();
        parcelObj.overallPrice = $(this).children('.par-price').text();

        parcelsArr.push(parcelObj);

    });

    consignmentObj.parcels = parcelsArr;

    consignmentsArr.push(consignmentObj);

我可能是一个n00b,但是这段代码(虽然我认为它相当冗长)应该有效。

$(this).children是否直接在每个()行上识别它正在迭代?

当我添加console.log(consignmentsArr);我得到对象内的数组应该是,但parcel对象的值只是重复表的最后一行。

1: Object
  deliName: ""
  deliStreet: ""
  docType: "Document"
  insurance: "None"
  parcels: Array[2]
    0: Object
      chargeValue:"R34.43"
      overallPrice:"R43.54"
      parcelId:"2"
      totalWeight:"65 kg's"
      volWeight:"63 kg's"
    1: Object
      chargeValue:"R34.43"
      overallPrice:"R43.54"
      parcelId:"2"
      totalWeight:"65 kg's"
      volWeight:"63 kg's"

为什么我不能将第一行值添加到parcels [0]?

由于

1 个答案:

答案 0 :(得分:1)

尝试在函数内声明parcelObj对象。

    $.each($('#parcels-added-overview tr'),function (index) {

        var parcelObj = new Object();

        parcelObj.parcelId = $(this).children('.par-id').text();
        parcelObj.totalWeight = $(this).children('.par-weight').text();
        parcelObj.volWeight = $(this).children('.par-vol-weight').text();
        parcelObj.chargeWeight = $(this).children('.par-charge-weight').text();
        parcelObj.overallPrice = $(this).children('.par-price').text();

        parcelsArr.push(parcelObj);

    });

    consignmentObj.parcels = parcelsArr;

    consignmentsArr.push(consignmentObj);