Я новичок в JavaScript.
У меня есть структура вложенных классов, где мне нужно инициировать использование объекта json. Мой вопрос в том, как я могу инициировать массив объектов EventDate и назначить this.dates в конструкторе CustomerEvents
export default class CustomerEvent {
constructor(customer_event: any) {
this.event_title = customer_event.event_title;
this.customer = customer_event.customer;
this.total_budget = customer_event.total_budget;
this.no_of_people = customer_event.no_of_people;
this.dates = /**array of new EventDate(customer_event.dates) **/;
}
event_title: string;
customer: Customer;
total_budget: number;
no_of_people: number;
dates: EventDate[];
}
class EventDate {
constructor(date: any) {
this.start_date = date.start_date;
this.end_date = date.end_date;
}
start_date: Date;
end_date: Date;
}
Если бы кто-нибудь мог мне в этом помочь, это было бы действительно полезно. Спасибо



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Просто назначьте новый пустой массив, например:
constructor(customer_event: any) {
this.event_title = customer_event.event_title;
this.customer = customer_event.customer;
this.total_budget = customer_event.total_budget;
this.no_of_people = customer_event.no_of_people;
this.dates = [];
}
Если вам нужно преобразовать входящий массив, вы можете сделать это:
...
this.dates = customer_event.dates.map(date => new EventDate(date));
...
Руководство по стилю Angular рекомендует с использованием interfaces для модели данных вместо classes:
Consider using an interface for data models.
При этом вы можете реорганизовать свой код следующим образом:
export interface EventDate {
start_date: Date;
end_date: Date;
}
export interface CustomerEvent {
event_title: string;
customer: Customer;
total_budget: number;
no_of_people: number;
dates: EventDate[];
}
Теперь, когда дело доходит до инициализации, вы можете сделать это примерно так:
const customerEvent: CustomerEvent = {
event_title: 'Some Title',
customer: { /*An Object representing a Customer Type*/ }
total_budget: 123,
no_of_people: 456,
dates: [{
start_date: new Date(),
end_date: new Date()
}]
};
Создайте эти экземпляры самостоятельно:
constructor(customer_event: any) {
this.event_title = customer_event.event_title;
this.customer = customer_event.customer;
this.total_budget = customer_event.total_budget;
this.no_of_people = customer_event.no_of_people;
this.dates = customer_event.dates.map(date => new EventDate(date));
}