Zakaria
Zakaria

Reputation: 15070

Updating array values (Appcelerator Titanium Mobile)

I'm using both the 1.5.1 and 1.6.0 to test my apps on my OS X.

I'm trying to update some values in this array:

var myTab = [
{title:'foo1',value:'bar1'},
{title:'foo2',value:'bar2'}
];

If I update a value field in this array, it doesn't do it (which isn't normal):

Titanium.API.info('Before :' + myTab[0].value);
myTab[0].value = 'updated!';
Titanium.API.info('After :' + myTab[0].value);

It displays 'bar1' instead of 'updated!'. What I tried next is to put the tab as a property list:

Titanium.App.Properties.setList('propTab',myTab);

And then, I tried to do the same thing:

Titanium.API.info('Before :' + Titanium.App.Properties.getList('propTab')[0].value);
Titanium.App.Properties.getList('propTab')[0].value = 'updated!';
Titanium.API.info('After :' +  Titanium.App.Properties.getList('propTab')[0].value[0].value);

Same result: It displays 'bar1' instead of 'updated!'.

Is there another solution?

Thank you,

Regards

Upvotes: 0

Views: 2576

Answers (2)

Anand
Anand

Reputation: 872

You need to mention it as an array element then it should work. For e.g., in your case you may modify as foolows

myTab[0]['value'] = 'updated!';

Upvotes: 0

Zach Snow
Zach Snow

Reputation: 1054

I've run into this sort of behavior before. While I never ascertained the cause, I found a workaround: you need to set myTab[0] to a value, and not myTab[0].value to a value. So, thus:

myTab[0] = {title: myTab[0].title, value: "updated!"};

Why, I do not know... I should probably figure it out.

Upvotes: 3

Related Questions