0

I'm using vue and typescript and I meet a problem, how to fix it? Here is my code:

private setTitle(systemConfig: any) {
    const systemConfigParse;
    let obj;
    systemConfigParse = JSON.parse(systemConfig);
    obj = (<any>systemConfigParse).find((item: any) => {
      return item.Code == "hospitalName";
    });
    this.hospitalName = obj.Value;
    obj = (<any>systemConfigParse).find((item: any) => {
      return item.Code == "systemName";
    });
    this.systemName = obj.Value;
    this.title = this.hospitalName + this.systemName;
  }

The error is at this row return item.Code == "hospitalName"; But after I deleting the code:

 obj = (<any>systemConfigParse).find((item: any) => {
          return item.Code == "hospitalName";
        });
 obj = (<any>systemConfigParse).find((item: any) => {
          return item.Code == "systemName";
        });

It still has error Is it the trick made by eslint? How to fix it? Thank you very much

2 Answers 2

1

You can simplify your code a lot:

public setTitle(systemConfig: string) {
    const systemConfigParse = JSON.parse(systemConfig);
    this.hospitalName = systemConfigParse.hospitalName;
    this.systemName = systemConfigParse.systemName;
    this.title = this.hospitalName + this.systemName;
}

Data would be shaped like this:

const jsonMock = `{"hospitalName":"hospital", "systemName": "system"}`;
yourInstance.setTitle(jsonMock);
Sign up to request clarification or add additional context in comments.

2 Comments

I can't simplify it like this because the input 'systemConfig' has other properties..So I can't change it to string
Then your own answer is the way to go, you should avoid to use any if you know the type (here a string) :)
0

I fixed it

private setTitle(systemConfig: any) {
    const systemConfigParse= JSON.parse(systemConfig);
    let obj;
    for(const item in systemConfigParse){
      const i = systemConfigParse[item];
      if(i.Code=="hospitalName"){
        this.hospitalName = i.Value;
      } else if(i.Code=="systemName"){
        this.systemName = i.Value;
      }
    }
    this.title = this.hospitalName + this.systemName;
  }

I used forof instead forin, thanks to @devdgehog

private setTitle(systemConfig: any) {
    const systemConfigParse = JSON.parse(systemConfig);
    let obj;
    for (const item of systemConfigParse) {
      if (item.Code == "hospitalName") {
        this.hospitalName = item.Value;
        console.log(this.hospitalName);
      } else if (item.Code == "systemName") {
        this.systemName = item.Value;
      }
    }
    this.title = this.hospitalName + this.systemName;
  }

1 Comment

Instead of using for(const item in systemConfigParse) consider using for(const item of systemConfigParse) (of) and use item directly, your code will be clearer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.