VMware Cloud Community
FTVDaniel
Contributor
Contributor
Jump to solution

work with array on vro 7.3

Hello,

I am migrating workflows from vco 5.3 to vro 7.3 and I have an issue on array management

the indexOf property seems to not work anymore

array.indexOf(member) gives -1 even if the member value is realy member of array

I want to check if an item is member of an Array but I have no way to test it

can some one help?

1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

This is the documentation for Javascript array filter() method - https://www.w3schools.com/jsref/jsref_filter.asp

filter() method on a given array gets a test function and produces an array with elements that pass the test function. In the sample code, the test function is:

  function (item) { return item.about.instanceUuid == vc.about.instanceUuid}

Here, item is the array element that will be tested (filter() will call the test function in a loop over all array elements). If the execution of the test function return true for the given item, the item will be included in the array produced as result from filter() call; if the test return false, the item will be skipped.

The sample code checks that the length of the result array is equal to 1, as the assumption it makes is that only one element from the array will have the desired instanceUuid value.

View solution in original post

5 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

What kind of objects are stored in the array? If they are vCenter plug-in objects (eg. array of VC:VirtualMachine), then this is a known 'effect' from the new vCenter plug-in implementation. See https://communities.vmware.com/message/2687455?et=notification.mention#2687455 for details.

0 Kudos
FTVDaniel
Contributor
Contributor
Jump to solution

Hello I am not sure to understand the solution proposed in the post you sent me

I have understood that the new plugin does not store vc objects the same way as before in the arrays so the index of using an object does not give result

and the solution is so to use a property of the object

did I understood well?

the fact is that I do not understand the code bellow(do not know how to use it for my use) and the only way I know to find if the name of a datastore is included on a storagePod is to use a loop but I want to avoid loops

The problem can be solved with changing the script to compare meaningful properties of the objects, as tschoergez correctly proposed bellow :

1 == attrVCs.filter(function (item) { return item.about.instanceUuid == vc.about.instanceUuid}).length

0 Kudos
FTVDaniel
Contributor
Contributor
Jump to solution

so after tests

if storagePod is a vc:storagePod object

and datastore is a vc:datastore object

this give true if datastore is part of the storagepod

System.log(1 == storagePod.childEntity.filter(function (item) { return item.name == datastore.name}).length);

but if storagePod is an array of storagePods

and datastore is a vc:datastore object

this gives false everytime

System.log(1 == storagePod.filter(function (item) { return item.name == datastore.name}).length);

but surely I am wrong using this code cause I do not understand it Smiley Happy

here is my original action that works in vco 5.3 and does not work in vro 7.3

// getallstoragepodsofcluster

storagePods = new Array()

for each (datastore in cluster.datastore){

//System.log(datastore.name);

if (datastore.parent.reference.type == "StoragePod"){

currentStoragePod = datastore.parent

//System.log(currentStoragePod.name);

if (storagePods.indexOf(currentStoragePod) === -1){

storagePods.push(currentStoragePod)

}

}

}

return storagePods;

iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

This is the documentation for Javascript array filter() method - https://www.w3schools.com/jsref/jsref_filter.asp

filter() method on a given array gets a test function and produces an array with elements that pass the test function. In the sample code, the test function is:

  function (item) { return item.about.instanceUuid == vc.about.instanceUuid}

Here, item is the array element that will be tested (filter() will call the test function in a loop over all array elements). If the execution of the test function return true for the given item, the item will be included in the array produced as result from filter() call; if the test return false, the item will be skipped.

The sample code checks that the length of the result array is equal to 1, as the assumption it makes is that only one element from the array will have the desired instanceUuid value.

FTVDaniel
Contributor
Contributor
Jump to solution

thanks a lot this will help Smiley Happy

0 Kudos