0

I want to get service API data into component class and manipulate data before sending to html. Below is my component class code,

  discountlists:Autodiscount;
  filterdata:any;

onChangeCountry(id: string){
  if(id == 'none'){
    alert("No Code Selected");
  }else if(id != null || id != ''){
    this.filterdata = this.autopostService.getDiscountSchemesById(id).subscribe(
      (data) => this.discountlists = data,
      error => this.error = error
    );
    alert(this.filterdata);
  }
}

Here i am storing values in filterdata which is type any and i am getting values from service api as below,

{
   "discount_id": "1",
   "discount_code": "SAVE20",
   "discount_price": "10%",
   "start_discount_date": "2019-12-17T00:00:00.000+0000",
   "end_discount_date": "2019-12-30T05:10:45.680+0000",
   "discount_on_car": true,
   "discount_on_part": false,
   "discount_on_All": false,
   "discount_on_cars": [
       "Nissan"
   ],
   "discount_on_parts": [
       ""
   ]
}

above value is being cast into a class file discountlists:Autodiscount; I want to perform some operations from above values and then i want to display that value into html. As i am getting values in [object Object] how can i cast this into string?

nilay
  • 43
  • 8

3 Answers3

0

To get the value, first access the property by this.data.discount_price, to get the numeric number, replace the character % and then cast it to numeric value using parseFloat

Try like this:

let discountPercent = parseFloat(this.data.discount_price.replace("%", ""));

Working Demo

Adrita Sharma
  • 21,581
  • 10
  • 69
  • 79
0

Modify your this.discountlists object (perform the arithmetic operations), so it can be accessed in the HTML

onChangeCountry(id: string){
  if (id == 'none') {
    alert("No Code Selected");
  } else if (id != null || id != '') {
    this.filterdata = this.autopostService.getDiscountSchemesById(id).subscribe(
      (data) => {
        this.discountlists = data;
        this.discountlists.discount_price = parseFloat(data.discount_price.replace('%', ''));
      },
      error => this.error = error
    );
    alert(this.filterdata);
  }
}
Ininiv
  • 1,325
  • 7
  • 12
0

You can do this in the following way:

discountlists:AutoDiscount;

  filterData:any; // you can also use the model instead of any like:
// Define this under Core part, you can create a interfaces folder there and use it via adding reference to common file i.e index.ts
    export interface AutoDiscount{
      discount_id: number
      discount_code: string;
      discount_price: string;
      start_discount_date: string;
      end_discount_date: string;
      discount_on_car: boolean;
      discount_on_part: boolean;
      discount_on_All: boolean;
      discount_on_cars: Cars[];
      discount_on_parts: Parts[];
    }

    export interface Cars
    {
      Name: string;
    }

    export interface Parts
    {
      Name: string;
    }

then filterData: AutoDiscount;

    onChangeCountry(id: string){
      if(id == 'none'){
        alert("No Code Selected");
      }else if(id != null || id != ''){
        this.autopostService.getDiscountSchemesById(id).subscribe(
        (result: any) => {
          if (result.items.length) {
// Define another method in autopostService i.e formatItems to format the items of result object. You can create a business logic as per your requirement to change/update the existing JSON detail
this.filterdata = this.autopostService.formatItems(result); 
}
 });    
}

There is no need to create two different object to filter the data i.e discountlists:Autodiscount; you can do/manipulate data using one object either filterData or discountLists

Jayoti Parkash
  • 868
  • 11
  • 26