Sunday, 7 April 2019

Mapping between two arrays of observable classes

I have two given classes SgFormsBaseand QuestionBase with slightly different member names, and I want to translate observable[] of one to the other.

import { of, Observable } from 'rxjs'
import { map} from 'rxjs/operators'

class SgFormsBase{
  constructor(
     public id: number,
     public survey: string
  ){}
}

class QuestionBase{
  constructor(
     public qid: number,
     public qsurvey: string
  ){}
}

const a = new SgFormsBase(11, 'Endo')
const b = new SgFormsBase(12, 'Kolo')
const sg = of([a, b] )

function toQuestionsBase(sgforms: Observable<SgFormsBase[]>): Observable<QuestionBase[]> {
  return sgforms.pipe(map(
     sgform =>  new QuestionBase(sgform.id, sgform.survey)))
}

toQuestionsBase(sg)



from Mapping between two arrays of observable classes

No comments:

Post a Comment