简体   繁体   English

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

[英]my array remains 'undefined' after array.push()

this is my code 这是我的代码

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);

My the array i want to push : 我想推送的数组:

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: ''}
];

When i load the data out of the array(roctje) all it'll say inside the app is Undifined. 当我从array(roctje)中加载数据时,它在应用程序中的所有内容都将显示为Undifined。 My guess is that the array is empty, not sure though. 我的猜测是该数组为空,虽然不确定。 Can anyone help me out :p? 谁能帮助我:p? Thanks in advance 提前致谢

Not sure from the question, but from observation I can see in the for loop u have used data as an array instead of arr which is the array. 从问题上不能确定,但​​是从观察中我可以看到,在for循环中,您已经将data用作数组而不是数组arr

data is an object which you have passed to get arr . data是您为了获取arr而传递的对象。

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

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