获取对象数组中对象属性的值

时间:2015-11-14 10:31:19

标签: javascript jquery javascript-objects

我有一些对象:

var ads = [
{   
    adName: "Football Team",
    adText: ["....","...."],
    adImg: ["images/img1.png", "images/img2.png", "", "", ""],
    adPreviewLink: "templateA",
    adSeconds: 15,
    adTime: ad1Time
},{
    adName: "Basketball Team",
    adText: ["...","..."],
    adImg: ["images/img-b1.png", "images/img-b2.png", "", "", ""],
    adPreviewLink: "templateB",
    adSeconds: 10,
    adTime: ad2Time

}];

adTime属性也是一个对象:

var ad1Time = {
    hours: ["6","12"],
    day: [weekdaysEnum.monday, weekdaysEnum.wednsday],
    month: [monthEnum.march, monthEnum.april],
    year: 2016,
    daysNumber: []
}

现在我想检查adTime>>年,但我收到了错误。

  

未捕获的TypeError:无法读取属性'年份'未定义的

以下是我试图检查财产的方法:

var date = new Date();
var now_day = date.getDate();
var now_month = date.getMonth();
var now_year = date.getFullYear();

for (var i = 0; i < ads.length; i++) {
    if (ads[i].adTime.year == now_year) {
        for(var index_month; index_month < ads[i].adTime.month.length; index_month++)
        {
            if(ads[i].adTime.month[index_month] == now_month)
                console.log(ads[i].adName);
        }
    }

}

那么错误在哪里(这里的错误:ads [i] .adTime.year)?感谢。

1 个答案:

答案 0 :(得分:2)

您需要定义&#39; ad1Time&#39;和&#39; ad2Time&#39;之前&#39;广告&#39;。