2016-11-18 5 views
2

중첩 시도 중 2 _.forEach;_.forEach 루프 값 모두 상속 된 데이터 변경

_.forEach(this.operationTypes, (value: any, key) => { 
    value.Terms = []; 
    this.operationLimits.push(value); 
    _.forEach(this.operationTerms, (subValue: OperationTerm, subKey) => { 
    let typeTerms = _.filter(this.operationTypeTerms, { OperationType: { Id: value.Id }, OperationTerm: { Id: subValue.Id } }); 
    subValue.TypeTerms = typeTerms[0]; 
    this.operationLimits[key].Terms.push(subValue); 
    }); 
}); 

그러나 모두 TypeTerms 값이 마지막 루프 값과 동일하게 생성됩니다. 즉, subValue이 변경되면 foreach 루프 subValue에서 새 값이 상속된다는 의미입니다.이 모든 값은 자동으로 변경됩니다.

어떻게 방지 할 수 있습니까?

감사

편집 :이 같이 있어야

. operationTerms의 PARAMS 입력 모르게

[ 
{ 
Active: true, 
Id: 2, 
Name: "Cash Out", 
Terms: [ 
    { 
    Id: 2, 
    Name: "Daily Limit", 
    TypeTerms: "Forbidden" -> all of these are same 'forbidden', but it could be 'forbidden' or 'allowed'. At the end of the loop all data changed to forbidden, because the last datas TypeTerms value is 'forbidden' 
    }, 
    { 
    Id: 3, 
    Name: "Weekly Limit", 
    TypeTerms: "Forbidden" 
    } 
] 
}, 
{ 
Active: true, 
Id: 3, 
Name: "Top Up", 
Terms: [ 
    { 
    Id: 2, 
    Name: "Daily Limit", 
    TypeTerms: "Forbidden" 
    }, 
    { 
    Id: 3, 
    Name: "Weekly Limit" 
    TypeTerms: "Forbidden" 
    } 
] 
} 
] 
+3

두 중첩 루프에 영향을 미치지 않습니다 간단한 여기에 밑줄없이 각하지만 를 사용한 적이 있습니다 및 필터 후보처럼 보인다 리팩터링을위한 적은 양의 입력 데이터와 원하는 출력을 제공 할 수 있습니까? – jmargolisvt

+0

기본 항목 업데이트 –

답변

1
  • , operationTypes & operationTypeTerms는, 그 힘들은 필요한 출력을 생성한다. 그러나 질문 로직을 기반으로 & 제공된 입력을 가정하면 에 대한 결과를 가져와야합니다.
  • 내가 논리 그럼에도 불구하고

operationTypes = [{ 
 
    "Active": true, 
 
    "Id": 2, 
 
    "Name": "Cash Out" 
 
}, { 
 
    "Active": true, 
 
    "Id": 3, 
 
    "Name": "Top Up" 
 
}] 
 

 
operationTerms = [{ 
 
    Id: 2, 
 
    Name: "Daily Limit" 
 
}, { 
 
    Id: 3, 
 
    Name: "Weekly Limit" 
 
}] 
 

 
operationTypeTerms = [{ 
 
    "operationTypeId": 2, 
 
    "operationTermId": 2, 
 
    TypeTerms: ["Forbidden"] 
 
}, { 
 
    "operationTypeId": 3, 
 
    "operationTermId": 3, 
 
    TypeTerms: ["Allowed", "Forbidden"] 
 
}, { 
 
    "operationTypeId": 3, 
 
    "operationTermId": 2, 
 
    TypeTerms: ["Forbidden"] 
 
}, { 
 
    "operationTypeId": 2, 
 
    "operationTermId": 3, 
 
    TypeTerms: ["Allowed", "Forbidden"] 
 
}] 
 

 

 
var result = []; 
 
_.each(operationTypes, function(type) { 
 
    var typeObj = _.extend(type, {}); 
 
    var terms = []; 
 
    _.each(operationTerms, function(term) { 
 
    var val = _.extend(term, {}); 
 
    var typeTermObj = _.filter(operationTypeTerms, { 
 
     operationTypeId: type.Id, 
 
     operationTermId: term.Id 
 
    }); 
 
    val.TypeTerms = typeTermObj[0].TypeTerms[0]; 
 
    terms.push(val); 
 
    }); 
 
    typeObj.Terms = terms; 
 
    result.push(typeObj) 
 
}); 
 

 
$("#result").html(JSON.stringify(result))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 
 
<div id="result"></div>

+0

예. 그것은 작동합니다! operationTypeId : type.Id, operationTermId : term.Id와 같은 새로운 인스턴스를 만드는 모든 중요한 포인트 –