从HTML表中的JSON数组中的BY组

时间:2014-10-24 21:51:58

标签: javascript jquery

我有这个表,我正在尝试遍历tr并使用这样的数据获取ID:



 var array = [];
    var res = $(".seq").map(function () {
        
        return {
            Id: $('.Id', this).val(),
            DR: $('.DR', this).val(),
            CR: $('.CR', this).val()
        };
    }).get();

    var json = JSON.stringify(res, null, 3);
    alert(json);

<table>
  <tr class="seq">
    <td>
      <select class="Id">
        <option value="1" selected="selected">row 1</option>
        <option value="2">row 2</option>
        <option value="3">row 3</option>
        <option value="4">row 4</option>
</select>
    </td>
     <td> 
       <input class="DR" value="10" type="text">
     </td>
    <td> 
       <input class="CR" value="10" type="text">
     </td>
  </tr>
  <tr class="seq">
    <td>
      <select class="Id">
        <option value="1" selected="selected">row 1</option>
        <option value="2">row 2</option>
        <option value="3">row 3</option>
        <option value="4">row 4</option>
</select>
    </td>
     <td> 
       <input class="DR" value="30" type="text">
     </td>
    <td> 
       <input class="CR" value="5" type="text">
     </td>
  </tr>
</table>
&#13;
&#13;
&#13;

结果是这样的:

[
   {
      "Id": "1",
      "DR": "10",
      "CR": "10"
   },
   {
      "Id": "1",
      "DR": "30",
      "CR": "5"
   }
]

问题是我如何在数组中存在BY ID,如果存在,我想将值添加到CR和DR ......如下:

  [
   {
      "Id": "1",
      "DR": "40",
      "CR": "15"
   }
]

2 个答案:

答案 0 :(得分:0)

您可以过滤具有相同值的select元素,然后遍历文本输入。以下代码段应适用于多行。

var array = [],
    ids = [],
    $select = $(".Id");

var res = $(".seq").map(function () {
    var id = $('.Id', this).val();

    if ($.inArray(id, ids) > -1) return;

    ids.push(id);
    var $m = $select.filter(function () {
        return this.value === id;
    });

    var DR = 0, CR = 0;

    $m.closest('td').siblings().find('.DR, .CR').each(function () {
        if ($(this).hasClass('DR')) DR += +this.value;
        else CR += +this.value;
    });

    return {
        Id: id,
        DR: DR,
        CR: CR
    };
}).get();

http://jsfiddle.net/syo26tcs/

答案 1 :(得分:0)

你也可以查看这个:

    // the objects array as string (then will be converted to json ) 
    var arr = '[{"Id":"1","DR":"20","CR":"10"}, {"Id":"1","DR":"30","CR":"40"}, {"Id":"2","DR":"20","CR":"10"}, {"Id":"1","DR":"40","CR":"50"} ]';

    var idsArray = [];

    var length = 0;
    var newJsonData = [];
    var tmpObj;

    function objNumber(str){
      var length = 0; 
      str.replace(/\{(.+?)\}/g,function(match){
         length++;
      });
      return length;
    }

   length = objNumber(arr); 

    var jsonData = window.JSON.parse(arr);

    groupedArray = [];
    for(var i=0;i<length;i++){
      // 1:  grab all the ids and put them in the idsArray 
      if(idsArray.indexOf(jsonData[i].Id)  == -1){
        // idsString +='{"id":"'+jsonData[i].Id+'"}'});
        idsArray.push(jsonData[i].Id);
      }  

    }

    for(var i=0;i<idsArray.length;i++){
        tmpObj = {};
        tmpObj.Id = idsArray[i];
        tmpObj.DR = 0;
        tmpObj.CR = 0;
        newJsonData.push(tmpObj);
    }



    for(var i=0;i<idsArray.length ;i++){
        for(var j=0;j<length;j++){
            if(jsonData[j].Id == idsArray[i]){

                newJsonData[i].DR += Number(jsonData[j].DR);
                newJsonData[i].CR += Number(jsonData[j].CR);
            }

        }
    }


    console.log(idsArray);
    console.log(newJsonData); // newJsonData will contains the desired result