缺少JSON对象中的模型属性

时间:2018-04-26 09:53:51

标签: javascript json angular

我有3个型号:

export class Period {
    lifeYears: number;
    lifeDevelopment: number;
}

export class PairYearQuantity {
    id: number;
    year: number;
    quantity: number;
}

export class MarketingPhase {
    status: string;
    period: Period;
    investments: PairYearQuantity[];
    costs: PairYearQuantity[];
}

我创建了一个对象:

const marketingPhase: MarketingPhase = {
    status: 'empty',
    period: {
        lifeYears: 3,
        lifeDevelopment: 2
    },
    investments: [
        {
            id: 1,
            year: 2018,
            quantity: 15
        },
        {
            id: 2,
            year: 2019,
            quantity: 8
        }
    ],
    costs: [
        {
            id: 7,
            year: 2018,
            quantity: 21
        },
        {
            id: 8,
            year: 2019,
            quantity: 13
        }
    ]
};

如果我在控制台中打印对象,则缺少“id”属性,“quantity”的值为null。似乎PairYearQuantity模型效果不佳。

console.log(marketingPhase);

结果如下:

{
    status: 'empty',
    period: {
        lifeYears: 3,
        lifeDevelopment: 2
    },
    investments: [
        {
            year: 2018,
            quantity: null
        },
        {
            year: 2019,
            quantity: null
        }
    ],
    costs: [
        {
            year: 2018,
            quantity: null
        },
        {
            year: 2019,
            quantity: null
        }
    ]
}

但如果我打印console.log(marketingPhase.investments[0].quantity);,我会得到15

知道这是怎么回事以及如何解决的?

0 个答案:

没有答案