cancel
Showing results for 
Search instead for 
Did you mean: 

Using the BuiltIn.CreateVariantArray Method?

SOLVED
Highlighted
Community Leader

Re: Using the BuiltIn.CreateVariantArray Method?

Sorry it's my fault, in same function it cant work as i've written because when you use push it use variable reference and not value !

 

So tthe correct way is:

function Get_Menu_Cell_Test()  {
  var myArray = [];
  var myItem = {};
  let clone;
  myItem.Name = "Name 1";
  myItem.X = "0";
  myItem.Y = "1";
  clone = JSON.parse(JSON.stringify(myItem));
  myArray.push(clone);
  myItem.Name = "Name 2";
  myItem.X = "1";
  myItem.Y = "1";
  clone = JSON.parse(JSON.stringify(myItem));
  myArray.push(clone);
  myItem.Name = "Name 3";
  myItem.X = "2";
  myItem.Y = "1";
  clone = JSON.parse(JSON.stringify(myItem));
  myArray.push(clone);
}

BTW instead of array.push(value) you can do array[index] = value if you prefer;

 

 

Un sourire et ça repart

Highlighted
Contributor

Re: Using the BuiltIn.CreateVariantArray Method?

 


@BenoitB wrote:

Sorry it's my fault, in same function it cant work as i've written because when you use push it use variable reference and not value !

 

So tthe correct way is:

function Get_Menu_Cell_Test()  {
  var myArray = [];
  var myItem = {};
  let clone;
  myItem.Name = "Name 1";
  myItem.X = "0";
  myItem.Y = "1";
  clone = JSON.parse(JSON.stringify(myItem));
  myArray.push(clone);
  myItem.Name = "Name 2";
  myItem.X = "1";
  myItem.Y = "1";
  clone = JSON.parse(JSON.stringify(myItem));
  myArray.push(clone);
  myItem.Name = "Name 3";
  myItem.X = "2";
  myItem.Y = "1";
  clone = JSON.parse(JSON.stringify(myItem));
  myArray.push(clone);
}

BTW instead of array.push(value) you can do array[index] = value if you prefer;

 

 


 

Thanks, @BenoitB I will give this a try.  I have never used "clone = JSON.parse(JSON.stringify(myItem));"  is this simply turning the list myItems into a JSON string?

Highlighted
Community Leader

Re: Using the BuiltIn.CreateVariantArray Method?

it does both, put your object myItem in a json string (stringify) and after revert it to an object (parse), avoiding thus the reference problem.

This reference problem occurs only because myItem and myArray are populated into the same scope (function).

 

Un sourire et ça repart

Contributor

Re: Using the BuiltIn.CreateVariantArray Method?

Our team lead came up with a function that uses two arrays and the join method to accomplish our task.  I will probably say @rajulapati gave the 'Best Answer' for showing me how the Builtin.ArrayVariantArray method worked although it did not fit with what we needed to do.  Big thanks however to @BenoitB for sticking with me! I learned a few nice things from the code you presented.  

 

It's odd what sent me down this bunny trail.  I created an array in a function and then passed that array back to the main function.  When I tried to view it in 'Locals' in the main function I recieved an odd message (below) so I thought something had gone wrong.  It looked fine in the function I created it in.  Our team lead discovered that if you use 'Add Watch' you can verify the data in the returned array object from the main function and it looks correct.  I will post some snips of what was done later today.  

 

 

ArrayMessage.png

Highlighted
Community Leader

Re: Using the BuiltIn.CreateVariantArray Method?

Front of a problem, several paths can give you different solutions and sometimes the unexpected path give you an unattended but better solution Smiley Happy

It's a pleasure to share and do community work.

Un sourire et ça repart

Highlighted
Contributor

Re: Using the BuiltIn.CreateVariantArray Method?

@BenoitB @rajulapati 

 

In it's most basic form, here is a mock of what we ended up doing.

There were some arrayMenu.pops as needed to build the menu string as represened in Excel but this is an example of building the arrays.

 

Thanks again for the assist!

 

function ArrayTest()
{
  var arrayMain = [];  
  var arrayMenu = [];
  var intCount = 0;
  var intMainCount = 0;
  var intMenuCount = 0;
  
  for (intMainCount; intMainCount < 2; intMainCount++)
    {
    for (intMenuCount; intMenuCount < 5; intMenuCount++)
      {
      // This is where the menu name would be pushed into the array from Excel
      arrayMenu.push("Menu" + intMenuCount)
      }
arrayMain.push(arrayMenu.join(" |")) } intMainCount = 0; for (intMainCount; intMainCount < 2; intMainCount++) { Log.Message(arrayMain[intMainCount],""); } }