typescript, How do I pass an object to a constructor of class for instantiation

slideshowp2

I have an array object data get from backend api. like:

[
 {name: 'react', age: 4},
 {name: 'angular', age: 4},
 ...
 {name: 'rxjs', age: 2}
]

And I definite a class and an interface, like this:

interface IBook {
  name: string;
  age: number;
}

class Book{
  book: IBook;

  constructor(book: IBook) {
    this.book = book;
  }

  //I can definite some method here: 

  getFullName() {
    return this.book.firstName + ' ' + this.book.lastName;
  }

  isValid() {
    return book.name.length > 0;
  }

}

//when get the data
const dataModels = datas.map(data => {
  return new Book(data);
});

so I can encapsulate some data model methods like book.getFullName()

I can use it like this:

const fullname = book.getFullName()

rather than this:

const fullname = book.firstName + ' ' + book.lastName;

Is there a better way to do this? I am not sure my thought is the correct way or not.

The problem is how to convert a js object to ts class model according to a correct way.

Or, just definite the data interface. Is it necessary to convert javascript json data to typescript class model?

-- update --

especially, the nested data. like this:

const datas = [
 {name: 'react', age: 2, tag: [{id: 1}, {id: 2}]}
]
Rodris

If no methods are needed, just cast your data. Otherwise, you may copy the data to your class.

let datas = [
 {name: 'react', age: 4, extra: { value: 1 }},
 {name: 'angular', age: 4, extra: { value: 2 }},
 {name: 'rxjs', age: 2, extra: { value: 3 }}
]

interface IBook {
  name: string;
  age: number;
}

interface Extra {
    value: number;
}

let books: IBook[] = datas;
console.log(books[0].name); // react

class Book {
    name: string;
    age: number;
    extra: Extra;

    constructor(data: any) {
        for (let key in data) {
            this[key] = data[key];
        }
    }

    getFullName() {
        return this.name;
    }

    isValid() {
        return this.name.length > 0;
    }
}

let books2 = datas.map(book => new Book(book));
console.log(books2[1].getFullName()); // angular
console.dir(books2[0].extra.value); // 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How Do I Set a Breakpoint for Object Instantiation, with No Constructor?

From Dev

How can I pass an object I wrote into a constructor of another class?

From Dev

How do I prevent a instantiation of a template class?

From Dev

How do I allow for infinite object instantiation?

From Dev

How do I pass constructor parameters to imported class in ECMAScript 6?

From Dev

How can I do constructor overloading in a derived class in TypeScript?

From Dev

typescript how do i infer params of constructor to default class type?

From Dev

How do I define an typescript interface for a constructor that also is an object?

From Java

How do I pass a constructor to a constructor?

From Dev

constructor.function(constructor) How do you pass an object of the same class to a function?

From Dev

How to initialize a class and pass an object to the constructor in Android?

From Dev

How to pass a generic type object to a class constructor

From Dev

How can I pass a super-class object to a sub-class constructor?

From

How do I cast a JSON object to a typescript class

From Dev

How do I point a TypeScript property to a class object in a definition file?

From Dev

How do I pass a derived class constructor argument in a string to the base class?

From Dev

Java how do i declare a instance variable of another class if i need to pass a variable from the constructor

From Dev

How do I create a class with multiple instantiation options

From Dev

How do I pass values to this constructor?

From Dev

How do I pass an object?

From

How to build a TypeScript class constructor with object defining class fields?

From Dev

C# static class: should I pass the object to the constructor?

From Dev

How do I call an object of a class that inherits another class that has arguments in its constructor?

From Dev

How do I correctly pass a reference of a class object into another class-object in C++

From Dev

How do I pass a parameter into a class so that when its parameter is passed in the constructor the JFrame would move?

From Dev

How do I pass Fragment to class constructor by solving the error "Activity cannot be converted to Fragment"?

From Dev

How do I pass a value to a constructor and use it from a method in the same class?

From Dev

How do I pass a dynamic array of objects to a parameterized constructor of another class?

From Dev

Kotlin - How do I pass a function that has a class constructor with it's own parameter as a parameter of a composable?

Related Related

  1. 1

    How Do I Set a Breakpoint for Object Instantiation, with No Constructor?

  2. 2

    How can I pass an object I wrote into a constructor of another class?

  3. 3

    How do I prevent a instantiation of a template class?

  4. 4

    How do I allow for infinite object instantiation?

  5. 5

    How do I pass constructor parameters to imported class in ECMAScript 6?

  6. 6

    How can I do constructor overloading in a derived class in TypeScript?

  7. 7

    typescript how do i infer params of constructor to default class type?

  8. 8

    How do I define an typescript interface for a constructor that also is an object?

  9. 9

    How do I pass a constructor to a constructor?

  10. 10

    constructor.function(constructor) How do you pass an object of the same class to a function?

  11. 11

    How to initialize a class and pass an object to the constructor in Android?

  12. 12

    How to pass a generic type object to a class constructor

  13. 13

    How can I pass a super-class object to a sub-class constructor?

  14. 14

    How do I cast a JSON object to a typescript class

  15. 15

    How do I point a TypeScript property to a class object in a definition file?

  16. 16

    How do I pass a derived class constructor argument in a string to the base class?

  17. 17

    Java how do i declare a instance variable of another class if i need to pass a variable from the constructor

  18. 18

    How do I create a class with multiple instantiation options

  19. 19

    How do I pass values to this constructor?

  20. 20

    How do I pass an object?

  21. 21

    How to build a TypeScript class constructor with object defining class fields?

  22. 22

    C# static class: should I pass the object to the constructor?

  23. 23

    How do I call an object of a class that inherits another class that has arguments in its constructor?

  24. 24

    How do I correctly pass a reference of a class object into another class-object in C++

  25. 25

    How do I pass a parameter into a class so that when its parameter is passed in the constructor the JFrame would move?

  26. 26

    How do I pass Fragment to class constructor by solving the error "Activity cannot be converted to Fragment"?

  27. 27

    How do I pass a value to a constructor and use it from a method in the same class?

  28. 28

    How do I pass a dynamic array of objects to a parameterized constructor of another class?

  29. 29

    Kotlin - How do I pass a function that has a class constructor with it's own parameter as a parameter of a composable?

HotTag

Archive