2014-10-24 6 views
2

나는이 테이블이이 같은 데이터로 아이디를 TR을 통해 루프를 시도하고 얻을 수 있어요 :그룹 BY

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>

그 결과는 같다 이 :

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

문제 어떻게 할 수 있습니까 ID BY 그룹 배열에 존재하고 있다면 내가 이렇게 될 ... CR과 DR에 값을 추가 할 수있는 경우 :

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

답변

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/

+0

가 당신은 매우 환영 @BasharAl 내가 :) –

+0

필요 무엇 :) 대단히 감사합니다! :) – undefined

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 
+0

@ 하미즘 감사합니다 이것은 또 다른 좋은 답변입니다 :) –