Home > Enterprise >  Working with a function that returns an array
Working with a function that returns an array

Time:12-15

I have written a function in Javascipt that returns a variable sized array. How do I declare/assign a variable in the calling function to accept this array and perform further processing on it?

function myArrayFunction()
{
    const myArray = [];             //Initialise Empty Array
    var x = 0;                      //Array index (cannot use .push() function in my app)
    
    for (let i = 0 ; i < 6 ; i  ) {
        myArray[x] = i * 2;         //Simple function that doubles i and assigns value to index in array
    }
    return myArray;                 //Should return a 6 element array
}

const myOtherArray = [];            //The array for the value returned from myArrayFunction() to be stored in

myOtherArray = myArrayFunction();

I get a warning the Debugging Scripts.array declaration error: invalid assignment to const `myOtherArray'. What's wrong?

I have tried using the const, let and var key words to initialise an empty array value.

I was expecting the variable that is passed out of the function would be assigned to the variable within the calling function.

Updated Code

Here is updated code, which includes a console.log statement enclosed in a second for loop. The result I expect is that the function should loop back through myArray and print out each of the elements at its 6 index locations.

function myArrayFunction()
{
  const myArray = [];     //Initialise Empty Array
  var x = 0;              //Array index (

  for (let i = 0 ; i < 6 ; i  ) {
    myArray[x] = i * 2;   //Simple function  
  }
   for (i = 0; i < 6; i  ) { //Loop through array
       console.log(myArray[x]);  
   }
  return myArray;         //Should return a 6 element array
}

let myOtherArray = [];    //The array to store returned array

myOtherArray = myArrayFunction();

console.log(myOtherArray);

When I run this code, I do not see an array of 6 numbers, but only the last one (10)

CodePudding user response:

It seems that using the .push() function to create the array solves the problem

function myArrayFunction()
{
    const myArray = [];             //Initialise Empty Array
    var x = 0;                      //Array index (cannot use .push() function in my app)
    var y = 0;                      //The initial value of the array
    
    for (let i = 0 ; i < 6 ; i  ) {
        y = i * 2;
        myArray.push(y);            //push the answer onto array
        console.log("Value of myArray = "   myArray[i]);
    }
        
    return myArray;                 //Should return a 6 element array
}

It seems the error I am getting is not related to the Javascipt I had written, but rather the environment in which I am trying to execute it (Sparx Enterprise Architect). I will post on a more specialised forum and see if anyone has any ideas. Thanks all

CodePudding user response:

You have a typo in the variable name of Session.Output(classiferIDArray[0]); causing the classiferIDArray is not defined error.

  • Related