2016-11-18 34 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值與最後一個循環的值相同。 這意味着新的值從我的foreach循環subValue繼承,如果subValue更改,所有從此分配將自動更改。

如何防止?

感謝

編輯:

它應該是這個樣子。

[ 
{ 
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
  • 在不知道輸入PARAMS operationTerms的,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 –