简体   繁体   English

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

[英]Get value of object property inside array of objects

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:无法读取未定义的属性“年”

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)? 那么错误在哪里(这里的错误是:ads [i] .adTime.year)? Thanks. 谢谢。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从对象数组获取对象属性值 - Get object property value from array of objects 从数组内部的对象获取属性值 - Get property value from an object inside of an array 获取对象数组内的值,该值是另一个对象内的值 - get the value that is inside an array of objects which is a value inside another object JavaScript获得对象数组的值或属性(如果对象具有该属性) - JavaScript get value or property of an Array of objects if object has that property Javascript:如何为对象数组中的 object 属性设置值? - Javascript: How to set value to an object property inside an array of objects? 解析对象内部的对象以获得特定属性的数组? - Parsing objects inside object to get an array of a specific property? 根据对象的其他属性从对象数组内的对象数组中获取属性的“val” - Get `val` of property from array of objects inside array of objects based on other property of the object 获取对象数组内对象的给定键的值 - Get value of given key of object inside array of objects 对象数组内的特定对象的trim属性 - trim property of specific object inside array of objects 当我在同一对象中拥有另一个属性的值时,从对象数组中获取一个Object属性 - Get a Object property from array of objects when I have the value of another property in the same object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM