无法读取数组中最后一项上的"未定义"属性



我可以继续在我的数组中添加或删除记录,它会正常工作,直到它到达最后一个记录,即使该记录是它已经处理过的记录的精确副本。

数组:

[
{
    "adID": "1",
    "isDefault": "false",
    "adName": "Ad 1 Name",
    "adDesc": "Ad 1 Description",
    "adURL": "",
    "adFormat": "image",
    "adImages": {
        "adImageDSK": "../img/bgfour.jpg",
        "adImageDEV": "../img/bgfour.jpg"
    }
},
{
    "adID": "2",
    "isDefault": "true",
    "adName": "Ad 2 Name",
    "adDesc": "Ad 2 Description",
    "adURL": "",
    "adFormat": "image",
    "adImages": {
        "adImageDSK": "../img/bgnine.jpg",
        "adImageDEV": "../img/bgnine.jpg"
    }
},
{
    "adID": "3",
    "isDefault": "false",
    "adName": "Ad 3 Name",
    "adDesc": "Ad 3 Description",
    "adURL": "",
    "adFormat": "image",
    "adImages": {
        "adImageDSK": "../img/bgseven.jpg",
        "adImageDEV": "../img/bgseven.jpg"
    }
},
{
    "adID": "4",
    "isDefault": "false",
    "adName": "Ad 4 Name",
    "adDesc": "Ad 4 Description",
    "adURL": "",
    "adFormat": "image",
    "adImages": {
        "adImageDSK": "../img/bgfive.jpg",
        "adImageDEV": "../img/bgfive.jpg"
    }
}
]

j查询:

$.getJSON("../json/adverts.json", function (data) {
    "use strict";
    function loadAdData() {
        adArray = [];
        $.map(data, function (item) {
            adArray.push({
                'isDefault': item.isDefault,
                'adID': item.adID,
                'adName': item.adName,
                'adDesc': item.adDesc,
                'adURL': item.adURL,
                'adFormat': item.adFormat,
                'adImageDSK': item.adImages.adImageDSK,
                'adImageDEV': item.adImages.adImageDEV
            });
        });
    }
    loadAdData();
    var defArray = adArray;
    console.log(defArray);
    var newArray = [];
    $.each(defArray, function (i, item) {
        if (item.isDefault === 'true') {
            newArray.push({
                'adID': item.adID,
                'adName': item.adName,
                'adDesc': item.adDesc,
                'adURL': item.adURL,
                'adFormat': item.adFormat,
                'adImageDSK': item.adImageDSK,
                'adImageDEV': item.adImageDEV
            });
            defArray.splice($.inArray([i], defArray), 1);
        } else {
            console.log(item.adID);
        }
    })
}

实际数组的长度为 10 个项目,但它们是上述数字的复制和粘贴,数字发生了变化。我正在从文件中提取一些广告,并检查其中是否有任何必须显示的"默认值",将它们放在不同的数组中以供以后使用。

我认为您只想过滤数组。

function Advert(data) {
    this.isDefault = data.isDefault;
    this.adID = data.adID;
    this.adName = data.adName;
    this.adDesc = data.adDesc;
    this.adURL = data.adURL;
    this.adFormat = data.adFormat;
    this.adImageDSK = data.adImages ? data.adImages.adImageDSK : null;
    this.adImageDEV = data.adImages ? data.adImages.adImageDEV : null;
}
$.getJSON("../json/adverts.json").done(function (data) {
    var allAds = data.map(function (item) {
        return new Advert(item);
    });
    var defaultAds = allAds.filter(function (ad) {
        return ad.isDefault === 'true';
    });
    // ...
});

最新更新