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"
}
]
}
]
두 중첩 루프에 영향을 미치지 않습니다 간단한 여기에 밑줄없이 각하지만 를 사용한 적이 있습니다 및 필터 후보처럼 보인다 리팩터링을위한 적은 양의 입력 데이터와 원하는 출력을 제공 할 수 있습니까? – jmargolisvt
기본 항목 업데이트 –