Problem with arrays
I'm trying to make an array, but I'm unable to get the values of the array. When I put array[0] the code returns me the first value of the array, but to all the other parts of the array (array[1], array[2] array[n]) it returns me an error. Thanks

  • Hi Colin, for whatever reason: The calculation fields show only the first arr[0] and the last element arr[arr.length -1 ] w/o making them a string. All the other elements you get by using: String(arr[1])String(arr[2])... Rainer
  • I've run into a similar problem, and Marc didn't answer the issue here. Lets say i have defined `var arr = @all of created on` I know how many items there are in the referenced app, and sure enough, arr.length returns that number. However, attempting to call specific parts of the array fails: arr[0] - returns a result arr[1] - the script returned an undefined result arr[2] - the script returned an undefined result arr[n] - the script returned an undefined result   I would expect this if n was greater than arr.length-1, but it's not!   Also of interest: arr.join(",") does return (in the preview area) the expected string of all values. I've tested this with a variety of fields (not just 'created on', but also string and number fields), and I've done so in brand new calc fields (to avoid data type error). That said, I've also had times when this problem doesn't exist, so i'm really not sure what circumstances are causing it.  
  • Hi Marc, Thanks for info. I dont know how, but following code worked for me. var arr = []; for (var i = 0; i < @All of Year.length; i++) {if (@All of Year[i]!="") {arr.push(@All of Year[i])}} var sml = arr[0];var big = arr[0]; for(var i=0; i< arr.length; i++) { if(arr[i] > big) big = arr[i]; else if (arr[i] < sml) sml = arr[i]; }var lastyear= Number(big)
  • Hi Adil, This is most likely due to the fact that the sample value that Podio uses for `@All of years` is an array containing just a single item. You will therefore have to handle the case where the index that you're accessing in the array is out of bounds. Like so: arr[38] || null Depending on what kind of value your calculation field is returning, you may return your own appropriate default value such as: arr[38] || 'Could not calculate value' For a calc field returning a string. Or: arr[38] || 10 For a calc field returning a number. Hope that makes sense.
  • Hi Marcos, did you solve the problem? I have the same problem. Here is my code: ///code begins var arr = []; for (var i = 0; i < @All of years.length; i++) { arr.push(@All of years[i]);}arr[0]; ///code ends arr[0] gives 2012, it is OK. arr[1], or arr[2], etc  gives "the script returned an undefined result"  @All of years.length gives 4.
  • Hi Marcos, I think it would be helpful if you include your full code, so others can see what might be causing the problem :) /Jacquelyn - Podio
This question is closed.