2015-01-23 2 views
0

나는 여러개의 transacation 라인을 가지고 있으며 for 루프에서 각각을 처리 할 수 ​​있는지, 아니면 트랜잭션 ID가 참조되었는지를 확인하기 위해 조회 할 수있는 어레이에 적어도 저장할 수 있는지 알고 싶다. .고유 ID를 처리하고 확인하는 방법은 무엇입니까?

내 데이터는 다음과 같습니다

PersonID | TranID | LineID | Info 
123  | 1  | 0  | Apples 
123  | 1  | 1  | Oranges 
145  | 2  | 0  | Bananas 
145  | 2  | 1  | Popcorn 
145  | 2  | 2  | Mushrooms 

그리고 고유 구문 분석 싶은 것이 :

123 
#1 
Apples 
Oranges 

~~ 

145 
#2 
Bananas 
Popcorn 
Mushrooms 

을 내가 생각하고 있었는데 ..

var transaction = new Array(); 

for (var i = 0; datarows != null && i <= datarows.length; i++){ 

    if datarows[i].tranID is not in transaction{ 

    write the rows 
    transaction.push(datarows[i].tranID) 

    } else { 
    return; 
} 
} 

문제입니다 I 실제로 라인 ID를 저장하지 않으므로 각 개별 라인을 처리 할 수 ​​없습니다. 도움!

+0

혹시 jQuery를 사용하고 계십니까? –

+0

할 수는 있지만 현재는 단순한 자바 스크립트입니다. –

+1

코딩은 정상이지만 처리 방법이 꺼져 있습니다. 알고리즘이 잘못되었습니다. 'transaction' 배열에 저장하는 값은'personId'와'transactionId'에 대한 값을 포함하는 객체 ('{}')와'info' 문자열의 배열이어야합니다. datarows를 통과 할 때마다'transaction' 배열에 일치하는 레코드가 있는지 확인하십시오. (루프를 거치고'transactionId'에서 일치하는 것을 찾으십시오.) 만약 당신이 레코드가 아니라면, 레코드를 생성하고 그것을 푸시해야합니다. 배열,하지만 만약 당신이 다음 레코드를 검색하고 그것에 새로운 정보 문자열을 추가해야합니다. 덕분에 –

답변

1

좋아 .. 여기에 당신이해야 할 것입니다 (당신이 jQuery를 사용하고있는 가정 - 나는 보통 쉽게 찾을 수) -

var dataArray = [ 
 
    {personId: 123, tranId: 1, lineId: 0, info: 'Apples'}, 
 
    {personId:123, tranId:1, lineId:1, info:'Oranges'}, 
 
    {personId:145, tranId:2, lineId:0, info:'Bananas'}, 
 
    {personId:145, tranId:2, lineId:1, info:'Popcorn'}, 
 
    {personId:145, tranId:2, lineId:2, info:'Mushrooms'} 
 
]; 
 

 
//Don't bother about this 
 
$.each(dataArray, function(colIndex, item){ 
 
    
 
    var row = $('<tr/>'); 
 
    
 
    $.each(item, function(rowIndex, it){  
 
    row.append($('<td/>').text(it));  
 
    }); 
 
    
 
    $('#data tbody').append(row); 
 
}); 
 

 
// Have an array for filtered objects 
 
var filteredArray = []; 
 

 
// Iterate through data array 
 
$.each(dataArray, function(index, item){ 
 
    
 
    if(filteredArray.length == 0) 
 
    filteredArray.push(item); 
 
    
 
    // a flag to tell you if the data exists in the filtered array or not 
 
    var addItem = true; 
 
    
 
    // Check if the data exists in the filtered array 
 
    var filteredItem = $.grep(filteredArray, function(filItem, filIndex, filAll){ 
 
    
 
    // Here is where you check whatever conditions you want 
 
    if(filItem.personId == item.personId && filItem.tranId == item.tranId) { 
 
    addItem = false; 
 
     return false; 
 
    }  
 
    }); 
 
    
 
    // Add the data 
 
    if(addItem) 
 
    filteredArray.push(item); 
 
}); 
 

 

 
//Don't bother about this 
 
$.each(filteredArray, function(colIndex, item){ 
 
    
 
    var row = $('<tr/>'); 
 
    
 
    $.each(item, function(rowIndex, it){  
 
    row.append($('<td/>').text(it));  
 
    }); 
 
    
 
    $('#filteredData tbody').append(row); 
 
});
#data{ 
 
    border: grey 1px solid; 
 
} 
 

 
#filteredData{ 
 
    border: grey 1px solid; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="data"> 
 
    <thead> 
 
    <th>PersonID</th> 
 
    <th>TranID</th> 
 
    <th>LineID</th> 
 
    <th>Info</th> 
 
    </thead> 
 
    <tbody></tbody> 
 
</table> 
 
<br/><br/> 
 

 
<table id="filteredData"> 
 
    <thead> 
 
    <th>PersonID</th> 
 
    <th>TranID</th> 
 
    <th>LineID</th> 
 
    <th>Info</th> 
 
    </thead> 
 
    <tbody></tbody> 
 
</table>
또한

는, 여기에 CodePen입니다 - http://codepen.io/aswinramakrish/pen/YPQZBj