Adding elements in an array

amaradiswamy
Kilo Sage

Hi All,

I am struggling in adding elements of an array. I am using the below script.

while(ish.next())

  {

  lan1.push(ish.u_lan);

  }

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

{

  var lan2 = parseInt(lan1[i]);

  gs.log(lan2,"lan2");

  lan += lan2;

  gs.log(lan,"lan");

}

The above script is adding the only one value means in the log for "lan2" i am getting the same number , may be it is the last number in the array. Please help me to adding the numbers in the array successfully.

Thanks & Regards,

Swamy

1 ACCEPTED SOLUTION

Hi Kalai,



Thank you so much for your respone.



If i used .toString method then it is appending the value instead of summing up (like a[0] =1 and a[0] =2 then then if i perform sum operation it is giving 12 instead of 3). I have used Number(ish.u_lan), and it worked.



Regards,


Swamy


View solution in original post

3 REPLIES 3

Kalaiarasan Pus
Giga Sage

Use toString() or getValue



while(ish.next())


  {


  lan1.push(ish.u_lan.toString());


  }


Hi Kalai,



Thank you so much for your respone.



If i used .toString method then it is appending the value instead of summing up (like a[0] =1 and a[0] =2 then then if i perform sum operation it is giving 12 instead of 3). I have used Number(ish.u_lan), and it worked.



Regards,


Swamy


Kalaiarasan Pus
Giga Sage

And the legend himself mark.stanger had answered me. And, it was my first post/comment here



Bug while using background script