JukeboxHero
JukeboxHero

Reputation: 115

Put data from angular object to another

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

Upvotes: 1

Views: 128

Answers (1)

The Fabio
The Fabio

Reputation: 6250

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);
  })
});

Upvotes: 1

Related Questions