1

I have 2 arrays that has different Models

here is model of first array

export class TenancyTenantDto  {
name!: string | undefined;
email!: string | undefined;
paymentMethodName!: string | undefined;
paymentMethodId!: number | undefined;
propertyTenantId!: number | undefined;
isPrimary!: boolean | undefined;
id!: number | undefined;
}

and second array model

 export class TenancyTenantViewModel {
    name: string;
    email: string;
    paymentMethodName: string;
    paymentMethodId: number;
    tenancyTenantId: number;
    tenantId: number;
    isPrimary: boolean;
    id: Guid;


}

I need to map 1 array to 2. How I can do it?

I tried to do it like this this.tenants = [...tenants.map(e => new TenancyTenantViewModel({id: e.id, etc.}))];

but seems it not works.

1
  • in es6 I would do this.tenants = tenants.map( ({id, ...etc}) => Object.assign(new TenancyTenantViewModel(), {id, ...etc})) Commented Mar 6, 2020 at 10:33

1 Answer 1

1

Define your map:

function mapType(source: TenancyTenantDto): TenancyTenantViewModel {
     return { // Your mapping logic here }
}

Usage:

const originalArray: TenancyTenantDto[] = [...]
const mappedArray: TenancyTenantViewModel[] = originalArray.map(mapType); 
Sign up to request clarification or add additional context in comments.

Comments

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.