Skip to content
Advertisement

How to call multi variable array in JS function?

I’m using this function to select one item in drop down menu:

async selectNode(nodeType, nodeName) {
    await page.click(this.selectors.reset);
    await this.toggleNode(nodeType, nodeName, true);
    await this.select();
  }

Now I want to select multi item before “await this.select();” this step.

I tried to edit function like this:

async selectMultiNodes([nodeType, nodeName],) {
    await page.click(this.selectors.reset);
    for (var i = 0; i < nodeType.length; i++) {
      await this.toggleNode(nodeType, nodeName, true);
    }
    await this.select();}

but when I call this function to select multi item

await topologyBrowser.selectMultiNodes(['SGSN-MME', 'mme088'],['EPG', 'epg84-real'],['SAPC', 'sapc17']);

it has error

Expected 0-1 arguments, but got 3

How do I fix this?

Advertisement

Answer

You can make a method take an arbitrary number of arguments using the spread syntax ...

function tryMe(...args){
  console.log(args);
}

tryMe(1,2,3);

So you could make your method take an arbitrary number of arrays and deconstruct them in a loop

async function selectMultiNodes(...nodes) {
   for(let i=0;i<nodes.length;i++){
     const [nodeType,nodeName] = nodes[i];
     console.log(nodeType,nodeName);
   }
}
selectMultiNodes(['SGSN-MME', 'mme088'],['EPG', 'epg84-real'],['SAPC', 'sapc17']);

So your final code would be

async selectMultiNodes(...nodes) {
    await page.click(this.selectors.reset);
    for (var i = 0; i < nodes.length; i++) {
      const [nodeType,nodeName] = nodes[i];
      await this.toggleNode(nodeType, nodeName, true);
    }
    await this.select();
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement