Forum Discussion

funmay's avatar
funmay
Contributor
6 years ago
Solved

Return value in array

How can i return list of values into array.See example below var lists=[]; for(var i=0;i<=10;i++){                lists = 5 * i Log.Message(lists) } I want the return for the variable sho...
  • tristaanogre's avatar
    6 years ago

    Please read the following web page and associated tutorials on using Arrays in JavaScript.

    https://www.w3schools.com/js/js_arrays.asp

     

    Basically, while you defined the variable lists as an empty array, as soon as you assigned 5*i to lists, you changed the data type of the variable from array to integer.  JavaScript is not strongly typed so any variable can be anything and assignments like this will override the initial declaration.

     

    As I understand it, you want to assign to the i element of the array the value of i*5.  Here's the code with a screenshot of the output.

     

    function arrayProcessing(){
        var lists=[];
        for(var i=0;i<=10;i++){             
            lists[i] = 5 * i
            Log.Message("Array element " + i + " = " + lists[i])
        }
        Log.Message("The full array = " + lists)
    }

  • shankar_r's avatar
    6 years ago

    You can do like below,

     

    var lists=[];
    
    for(var i=0;i<=10;i++){             
    
      lists[i] = 5 * i;
    
    Log.Message(lists)
    
    }
    return lists;//if you want to return as array
    return aqConvert.VartoStr(lists);//if you want to return as string like "0,1,2,3,4"