İsmail Şener İsmail Şener - 12 days ago 5
TypeScript Question

_.forEach loop value change all inherited data

I'm trying nested 2 _.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);
});
});


But it creates all
TypeTerms
values same as last loop's values.
That means the new values are inherited from my foreach loops
subValue
, if the
subValue
changed, all assigned from this going to be changed automatically.

How to prevent that?

Thanks

Edit:

It supposed to be like this.

[
{
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"
}
]
}
]

Answer
  • Without knowing the input params of operationTerms, operationTypes & operationTypeTerms, its tough to produce the required output. However based on the question logic & provided output I assumed the input to bring the result you are seeking.
  • Please note that I've used simple underscore here and no angular but that shouldn't affect the logic nevertheless

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>

Comments