0

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

2 Answers 2

1

Below is how your function addExternalBudget should look like. Your find method is incorrect, inner expression should not have curly braces. If it has curly braces, you will have to return the value explicitly -

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){
      this.myChangeRequest.NewExternalBudget.push(ebCopy);
    }
    else
    {
      existingBudget.Cost=this.selectedExternalBudget.Cost+existingBudget.Cost;
    }
    
  }
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is your find. You can use filter like this to get the matched items:

let foundItems = this.myChangeRequest.NewExternalBudget.filter( _ => (_.TypeId == typeId && _.Year == yr))

So, here you get an array of matched object that if its length > 0 you got some answers. So keep processing on foundItems .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.