I have an array like this which is ofcourse dynamic and each of the element get pushed when clicking an add button next to few form fields
[{"EbudgetId":0,"CrId":0,"TypeId":"8","TypeName":"AUGMENT_STAFF","Year":2020,"Cost":456},
{"EbudgetId":0,"CrId":0,"TypeId":"8","TypeName":"AUGMENT_STAFF","Year":2020,"Cost":456},
{"EbudgetId":0,"CrId":0,"TypeId":"8","TypeName":"AUGMENT_STAFF","Year":2020,"Cost":456},
{"EbudgetId":0,"CrId":0,"TypeId":"8","TypeName":"AUGMENT_STAFF","Year":2021,"Cost":657}]
What my requirement is if there an Item which of a specific type and year already exists, then it should add with its value rather than pushing into the array.
With that intention I did like this but it doest work
addExternalBudget() {
let typeId:Number = this.selectedExternalBudget.TypeId;
let yr:number=this.selectedExternalBudget.Year;
if(typeId==0 || yr==0) return false;
let ebCopy: ExternalBudget = Object.assign({}, this.selectedExternalBudget);
let existingBudget:ExternalBudget=this.myChangeRequest.NewExternalBudget.find(x=>{x.TypeId===typeId;x.Year==yr});
if(existingBudget=== undefined){
this.myChangeRequest.NewExternalBudget.push(ebCopy);
}
else
{
existingBudget.Cost=this.selectedExternalBudget.Cost+existingBudget.Cost;
}
}
here even when the item already exists with given typeid and year, it getting added as a new item into the array.
What I did wrong here?
I am working in Angular 9