Я хочу вернуть объект с ключом date и 2 свойствами: margin и consultant.
!(moment(timesheet.date).format('YYYY') in this.grossMargin) ?
this.grossMargin[moment(timesheet.date).format('YYYY')] = timesheet.invoice.total - timesheet.purchase.total :
this.grossMargin[moment(timesheet.date).format('YYYY')] += timesheet.invoice.total - timesheet.purchase.total
this.grossMargin возвращает объект с year в качестве ключа и grossMargin в качестве значения.Теперь я хочу добавить еще один элемент в объект, например, общее количество консультанта.
Я пробовал это, но это не работает:
if (!(moment(timesheet.date).format('YYYY') in this.grossMargin)) {
this.grossMargin[moment(timesheet.date).format('YYYY')].margin = timesheet.invoice.total - timesheet.purchase.total
this.grossMargin[moment(timesheet.date).format('YYYY')].consultant = consultant.invoice.total - consultant.purchase.total
} else {
this.grossMargin[moment(timesheet.date).format('YYYY')].margin += timesheet.invoice.total - timesheet.purchase.total
this.grossMargin[moment(timesheet.date).format('YYYY')].consultant += consultant.invoice.total - consultant.purchase.total
}
Ошибка: невозможно установить свойство 'margin'из неопределенного