开发者

Accessing Array values dynamically

开发者 https://www.devze.com 2023-02-25 19:24 出处:网络
I have Array in this format. rowData[0] = addRow(aa); rowData[1] = addRow(aaa);开发者_JAVA百科 rowData[2] = addRow(aa);

I have Array in this format.

rowData[0] = addRow(aa);        
rowData[1] = addRow(aaa);     开发者_JAVA百科   
rowData[2] = addRow(aa);        
rowData[3] = addRow(aa);    

addRow is a function which gets this value process.But i don't want to give the Array Index, instead i want to give rowData[i], then put in a loop and access the elements.

rowData holds the an object which addRow returns.

var data = [rowData];
var table = Ti.UI.createTableView({
        data:data
});


Use the Array.push function to store the data:

rowData.push(addRow(aa));  
rowData.push(addRow(aaa));  
.
.
.
.
.

Another alternative is:

rowData[rowData.length] = addRow(aa);  
rowData[rowData.length] = addRow(aaa);  
.
.
.
.
.

Use the regular index based iterations to get the data:

for(var i=0; i< rowData.length; i++){
  var curItem = rowData[i];
}


for (var i = 0; i < rowData.length; i++)
{
    rowData[i] = addRow(aa);
}


did u mean this?

var rowData = {};
rowData[aa] = addRow(aa);
rowData[aaa] = addRow(aaa); 

for loop access

for(var index in rowData){
  var data = rowData[index]
  ...
}


A loop may not be feasible in your case. This may be an idea: you can rewrite the Array.push prototype method:

Array.prototype._push = Array.prototype.push;
Array.prototype.push = function(val){ this._push(val); return this;};

After which you can chain the push operations:

rowData.push(addRow(aa))
       .push(addRow(aaa))        
       .push(addRow(aa))        
       .push(addRow(aa)); 

But actually, it looks like you are mixing arrays with objects. Here's an answer I formulated earlier on that subject.

0

精彩评论

暂无评论...
验证码 换一张
取 消