Page 1 of 1

Sum a local storage variable array in Model

Posted: Wed Oct 05, 2016 8:22 am
by Paul Medawar

Hi,

I'm successfully using model and storage, however I am having trouble running through a storage variable for a sum.

I have a model which includes an object(OrderItem), which includes String(Item), Number(Qnt), Number(Cost).

I have another model which is the array of OrderItem - Array(OrderItemArray)

There are no issues with using and saving to the models.

However I am trying to sum the "Cost" by using the following

-----------------------------

var arr = Apperyio.storage.OrderItemArray.get("$['OrderItem']['Cost']");

var sum = 0;
for(i=0; i < arr.length ; i++)
sum = sum + arr;

Appery('mobilelabel_147').text(sum);
alert(sum);

-----------------------------

and receiving the following error

uncaught error: Attempt to access Array by property name: 'OrderItem'

Am I trying to access the model using the wrong method?


Sum a local storage variable array in Model

Posted: Wed Oct 05, 2016 10:46 am
by Serhii Kulibaba

Hello Paul,

Please use JS below:
prevar arr = Apperyio&#46;storage&#46;OrderItemArray&#46;get("$['Cost']");/pre
instead of:
prevar arr = Apperyio&#46;storage&#46;OrderItemArray&#46;get("$['OrderItem']['Cost']"); /pre


Sum a local storage variable array in Model

Posted: Wed Oct 05, 2016 11:52 am
by Paul Medawar

Thanks for the quick reply Sergiy,

worked with the following

var arr = Apperyio.storage.OrderItemArray.get("$['Cost']");

var sum = 0;
for(i=0; i < arr.length ; i++)
sum += parseFloat(arr);

Appery('mobilelabel_148').text(sum);
alert(sum);