S
S
sdgroup142019-11-01 18:59:46
JavaScript
sdgroup14, 2019-11-01 18:59:46

How to fill one array through another using the constructor for the object inside?

Objective: Get people out. For example, an array of objects comes with api, but there is a lot of extra stuff in them, + I want to add some methods through the prototype constructor. For example:

export class Person {

  constructor(
              private name,
              private sirname) {
    this.name = name;
    this.sirname = sirname;
  }

  getFullName() {
    return `${this.name} + ${this.sirname}`;
  }
}

And let's say I get an array of objects from the base:
response_persons = [
  {
   name: Vasya,
   sirname: Pupkin,
   id: 15,
   avatar: '/imgs/avatar.jpg'
  }
]

In order to sort it all out, I do this:
result_persons = []
response_persons.map(person => {
    result_persons .push(new Person(person.name, person.sirname))
})

Is there a way to fill an array through a constructor? to avoid overkill? To mine in php such pieces are. Those. I want to match each object in the response list and assign it all to a new array

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question