0

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:

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.

Uncaught TypeError: Cannot read property 'year' of undefined

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.

4
  • yes of course. it's look the same like ad1Time Commented Nov 14, 2015 at 10:33
  • code looks alright. Can you create a plunkr? Or maybe share the data... Commented Nov 14, 2015 at 10:33
  • 1
    man show us full code. u should first define a var and then use it. i guess u have defined ad1Time , ad2Time after ads Commented Nov 14, 2015 at 10:36
  • you are write. i defined the objects after the array of objects. Thanks! Commented Nov 14, 2015 at 10:43

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.