Is there a good way to map objects to other objects? Library recommendations welcome too.
For example, say I have these classes:
export class Draft {
id: number;
name: string;
summary: string;
}
export class Book {
id: number;
name: string;
info: Info;
}
export class Info {
value: string;
}
Traditionally, to map fields from Draft
to Book
I'd have to do so manually for each field:
export class Book {
[...]
fromDraft(Draft draft) {
this.id = draft.id;
this.name = draft.name;
this.info = new Info();
this.info.value = draft.summary;
}
}
Is there an easier way to map object fields that are named the same way? For example Draft
and Book
's id
and name
fields, while also being able to define custom mappings, like from draft.summary
to book.info.value
.
Note that this is far from my actual use case. Here it's not so bad to do assignments manually, but for objects with many similarly named fields it's quite a chore.
Thanks!