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

时间:2021-09-13 21:20:00

I have array of 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 property is also an object:

adTime属性也是一个对象:

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

now i want to check the adTime>>year, but I am getting an error.

现在我想查看adTime >> year,但是我收到了一个错误。

Uncaught TypeError: Cannot read property 'year' of undefined

未捕获的TypeError:无法读取未定义的属性“year”

Here is how i tried to check the property:

这是我试图检查财产的方式:

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);
        }
    }

}

So where is the mistake(the mistake here: ads[i].adTime.year)? Thanks.

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

1 个解决方案

#1


2  

You need to define 'ad1Time' and 'ad2Time' before 'ads'.

您需要在“广告”之前定义“ad1Time”和“ad2Time”。

#1


2  

You need to define 'ad1Time' and 'ad2Time' before 'ads'.

您需要在“广告”之前定义“ad1Time”和“ad2Time”。