我的阵列仍未定义'在array.push()之后

时间:2014-05-22 10:42:58

标签: javascript arrays titanium label push

这是我的代码

var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'mobile_db.js'); // Local File Read
Ti.include(f.nativePath);

var scrollView = Ti.UI.createScrollView({
    contentWidth : 'auto',
    contentHeight : 'auto',
    showVerticalScrollIndicator : true,
    showHorizontalScrollIndicator : false,
    layout : 'vertical',
    height : '100%',
    width : '100%',

});

function sortObject(obj) {
    var arr = [];
    for (var prop in obj) {
        if (obj.hasOwnProperty(prop)) {
            arr.push({
                'key' : prop,
                'value' : obj[prop]
            });
        }
    }
    arr.sort(function(a, b) {
        return a.value - b.value;
    });
    return arr;
}

var arr = sortObject(data);
var lblArr = [];
var roctje = [];

var alertDialog = Titanium.UI.createAlertDialog({
    title: 'ffs error?',
    message: 'Do you want to do this?',
    buttonNames: ['Yes','Sparta?','No'],
    cancel: 1
});

for (var i = 0; i < 20; i++) {
    if (data[i].instelling_title == "ROC") {
        roctje.push(data[i].instelling_title);
    }
    else if (data[i].instelling_title == "Scalda") {
        alertDialog.show();
    }
    else {
        alertDialog.show();
    }

    lblArr[i] = Ti.UI.createLabel({
        text : roctje[i] + ' ' + ' - ' + ' ' + ' ' + ' ' + adresArray[i].address_street + ' ' + adresArray[i].address_housenr + ' ' + adresArray[i].address_postalcode + ' ' + adresArray[i].address_city + ' ' + adresArray[i].address_country,
        color : 'black',
        top : '15',
        width : '85%'
    });
    scrollView.add(lblArr[i]);
}

var viewqq = Ti.UI.createView({
    borderRadius : 10,
    top : 10,
    height : 2000,
    width : 'auto'
});

Ti.UI.currentWindow.add(viewqq);
scrollView.add(viewqq);
Ti.UI.currentWindow.add(scrollView);
Ti.UI.currentWindow.add(viewqq);

我要推送的阵列:

var data = [
     {item: 'SCALDA 1', instelling_title: 'Scalda', instelling_desc: ''},
     {item: '14', instelling_title: 'Scalda', instelling_desc: ''},
     {item: '15', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '16', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '17', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '18', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '19', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: 'ROC22', instelling_title: 'ROC', instelling_desc: ''},
     {instelling_id: '21', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '22', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '13', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '12', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '3', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '4', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '5', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '6', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '799', instelling_title: 'ROC', instelling_desc: ''},
     {instelling_id: '8', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '9', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '10', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '11', instelling_title: 'Scalda', instelling_desc: ''},
     {instelling_id: '23', instelling_title: 'Scalda', instelling_desc: ''}
];

当我从数组中加载数据(roctje)时,它会在应用程序内部说是Undifined。我的猜测是阵列是空的,但不确定。任何人都可以帮助我:p? 提前致谢

1 个答案:

答案 0 :(得分:0)

从问题中不确定,但从观察中我可以看到在for循环中你使用data作为数组而不是arr这是数组。

data是您传递给arr的对象。