Is it possible to create a class instance from a plain object without manually mapping plain object values to class instance variables?
For example:
class Person {
id;
firstName;
lastName;
age;
fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
const hemmingway = {
id: 1,
firstName: "Ernest",
lastName: "Hemmingway",
age: 62
};
Is it possible to create a Person class instance out of hemmingway without having to resort to manually mapping the keys like this:
constructor(plainObject) {
this.id = plainObject.id;
this.firstName = plainObject.lastName;
this.lastName = plainObject.lastName;
this.age = plainObject.age;
}
This would be extremely helpful when trying to map network API response objects to class instances.