Kumara
Kumara

Reputation: 528

angular data modify before send the service

I need to modify my object. please check below function

    registerCus(item) {
    this.customer.code = 'B001';
    this.customer.avCode = 'L01';
    this.customer.ageCode = 'A1';
    this.registrationService.customerRequest(item).subscribe(data => {
       
    },
        error => {
            
        });
}

The item, included 3 values: code, avCode, ageCode. When I send the 'item' all three values pass to the service . According to my requirement I need to send only code and avCode. how can I modify 'item' before pass to service.

I am trying to do something like this,

registerCus(item) {
   item  = this.customer.code,  this.customer.avCode;
    this.registrationService.customerRequest(item).subscribe(data => {
    },
        error => {
            
        });
}

Upvotes: 0

Views: 52

Answers (2)

Harshit Rastogi
Harshit Rastogi

Reputation: 2122

If item and customer are different objects:

registerCus(item) {
    item.code  = this.customer.code;
    item.avCode  = this.customer.code;
    this.registrationService.customerRequest(item).subscribe(data => {
    },
        error => {
            
        });
}

Otherwise you can use Object de-structuring

Upvotes: 0

Apoorva Chikara
Apoorva Chikara

Reputation: 8773

You can use object de-structuring to achieve this behaviour without making any change in the code. You do this in your service method customerRequest;

customerRequest ({code, avCode}) { 
   console.log(code, avCode);
}

Learn more about destructuring in JS.

Upvotes: 2

Related Questions