new brain
new brain

Reputation: 19

How do to get data from object in angular to calculate percentage?

so I am really new to this and do not really know how to ask the question correctly, so please bear with me because I really want to understand this. So I got an object, Firms and this is the response that I get: enter image description here

In code it is looks like this:

  export interface Firm {
  name: string
  fullName: string
  ein: string
  crn: string
  email: string
  email2: string
  address: string
  address2: string
  zip: string
  city: string
  country: string
  phone: string
  phone2: string
  type: string
  logo: string
  disabled: true
  taxPayer: true
  locale: string
  currency: string
  domain: string
}

My question is how do I get "maxIncomePerYear" or "maxIncomePer365" from Firms so I can calculate with them? This is what I tried:

export class InsightsComponent implements OnInit {
  insights: any;
  firm:Firm;

  constructor(
    private firmService: FirmService,
    private provider: FirmProvider,
    private http: HttpClient,
  ) {}
  ngOnInit(): void {
    this.provider.getInsights(this.firmService.getDomain())
      .then(data => {
        console.log(data);
        this.insights = data;
      });
    this.provider.getCurrent(this.firmService.getDomain())
      .then(data => {
        this.firm = data;
      });
  }

  calcInvoiced = (firm: number, insights: number) => {
     

 return ((this.firms.maxIncomePerYear / this.insights.incomeForYear) * 100).toFixed(2);
  }
}

Upvotes: 0

Views: 84

Answers (2)

new brain
new brain

Reputation: 19

This is how I solved it:

export class InsightsComponent implements OnInit {
  insights: any;
  firm: any;
  

  constructor(
    private firmService: FirmService,
    private provider: FirmProvider,
    private http: HttpClient,
  ) {}
  ngOnInit(): void {
    this.provider.getInsights(this.firmService.getDomain())
      .then(data => {
        console.log(data);
        this.insights = data;
      });
    this.provider.getCurrent(this.firmService.getDomain())
      .then(data => {
        this.firm = data;
      });
  }

  calcInvoiced = (firm: any, insights: any, suffix: string): string => {
    if (!firm || !insights) {
      return '0';
    }  
    return (this.insights.incomeForYear / this.firm.maxIncomePerYear * 100).toFixed(2) + suffix;
  }
}

Upvotes: 0

Krishna Rathore
Krishna Rathore

Reputation: 9687

You are using wrong key for calculate percentage

calcInvoiced = (firm: number, insights: number) => {
    return ((this.firms.maxIncomePer365 / this.insights.incomeForYear) * 100).toFixed(2);
}

Upvotes: 0

Related Questions