繁体   English   中英

我的数组在array.push()之后仍然是'undefined'

[英]my array remains 'undefined' after array.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: ''}
];

当我从array(roctje)中加载数据时,它在应用程序中的所有内容都将显示为Undifined。 我的猜测是该数组为空,虽然不确定。 谁能帮助我:p? 提前致谢

从问题上不能确定,但​​是从观察中我可以看到,在for循环中,您已经将data用作数组而不是数组arr

data是您为了获取arr而传递的对象。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM