Search code examples
javascriptarraystitaniumlabelpush

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. My guess is that the array is empty, not sure though. Can anyone help me out :p? Thanks in advance


Solution

  • 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.

    data is an object which you have passed to get arr.