1

I get data from an API in the "Results" Type. What I need to do is to bring this Data to the Type "Export" to convert it to JSON.

export interface ExportModel {
  name: string;
  passedCombined: boolean;
  passedExp: boolean [];
}

export interface Results {
  user: {
     userid: string
     name: string
     unit: string
  };
  passedCombined: boolean;
  exp: {
    name: string
    passed: boolean
  }[];
}

I tried to solve this problem with the following code.

resultData: Observable<Results[]> = of();
export: ExportModel[] = [];

this.resultData = this.someservice.getData();

this.resultData.pipe(map(t => t.map(t => t.user.name))).subscribe(name => this.resultExport.name.push(name));

The last line drops the error "TS2339: Property 'name' does not exist on type 'ExportModel[]'"

The service, I getting the data from, looks like:

getData(): Observable<Results[]> {
    return this.http.get<Results[]>(`/backend/resultservice`);
  }

The background is I want to create a CSV looks like:

PassedCombined, Name, Experiment1, Experiment2,...,
true, Howard, true, false,...,
false, Amy, false, false,...,

The return of the Service looks like the following. I want to pick just a few values out of it, because I don't need all for my export. After I get my values I want to convert them to JSON and build a csv of it.

Is my approach feasible or is there an easier way to transfer the data into a csv file?

Output now:

[
   {
      "user":{
         "userid":"pst",
         "name":"PeterSmith",
         "unit":"admin",
      },
      "passedCombined":false,
      "exp":[
         {
            "name": "exampleExp",
            "passed":false,
         },
         {
            "name":"AnotherExp",
            "passed": true,
         }
      ]
   }
]

I want it looks like:

   [
       {
          "name":"PeterSmith",
          "passedCombined":false,
          "exp":[
             {
                "passed":false,
             },
             {
                "passed":true,
             }
          ]
       }
    ]

At the end I want to build a csv looks like with around 50-100 people:

false, PeterSmith, false, true

1 Answer 1

1

You could do the conversion when getting the data, by using some map calls

assuming your getData() results in an observable from a http call, this should do it:

this.someservice.getData().subscribe(results => {
  this.export = results.map(result => {
    name: result.user.name;
    passedCombined: result.passedCombined;
    passedExp: result.exp.map(x => x.passed);
  })
});

Sign up to request clarification or add additional context in comments.

5 Comments

On line two I get the error message "TS2322: Type 'void[]' is not assignable to type 'ExportModel[]'. Type 'void' is not assignable to type 'ResultExport'." and on the labled lines I get the error "TSLint: unexpected label on statement(label-position). Do you have an idea where it comes from?
probably from your getData function. it should be returning an "Observable<Results[]>", and seems to be returning "Observable<void[]>". Could you please update your question adding the body of function "getData" ?
I added the getData to the question body. If nothing works, I can build a new service in the backend to collect the data in the format of ExportModel. But I prefer to use the given getData function and convert these data into ExportModel because of performance reasons.
the service looks alright, my bet is now that the response itself might not be correct. Could you please add the response you get from your Backend (you can see it in the Browser's devtools) when getData is executed?
I added the output of the browser and give detailed information about what I want to do.

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.