Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How to split arguments using the loop?

zak100

Coder
Hi,

I want to separate arguments and their types as given in the following string:



"(address to, uint amount)"



The number of arguments are not restricted to 2. Thus I need a loop. For the above, I am doing without a loop using:

JavaScript:
argArr_TypeAndNameSeparated1=argArr_TypeAndNameCombined[0].split(' ')
argArr_TypeAndNameSeparated2=argArr_TypeAndNameCombined[1].split(' ')



I want to perform above command using a loop but I can’t understand how to use a 2d array. The complete code is given below:



Code:
const path = require("path");
const fs = require("fs");
module.exports = async function(callback)
{
try {
let argStr = "(address to, uint amount)"
let argArr_TypeAndNameCombined = []
let argArr_TypeAndNameSeparated= "" //[[], []]
argArr_TypeAndNameCombined = argStr.split(',');
find_the_arguments(argStr)
}
catch (error) {
console.log(error)
}
callback();
}
function find_the_arguments(argStr) {
   argArr_TypeAndNameCombined = argStr.split(',')
   //argArr_TypeAndNameCombined[0] = (address to
   //argArr_TypeAndNameCombined[1] = uint amount)
   argArr_TypeAndNameSeparated1=argArr_TypeAndNameCombined[0].split(' ')
   argArr_TypeAndNameSeparated2=argArr_TypeAndNameCombined[1].split(' ')
   for(let i=0; i<argArr_TypeAndNameSeparated1.length; i++){
      console.log("arg="+argArr_TypeAndNameSeparated1[i])
   }
   for(let i=0; i<argArr_TypeAndNameSeparated2.length; i++){
      console.log("arg="+argArr_TypeAndNameSeparated2[i])
   }
}

/*The output is:

Using network 'development'.
arg=(address
arg=
arg=to
arg=
arg=uint
arg=amount)
*/



I also want to replace the last two for loops using a single loop. Somebody please guide me.



Zulfi.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom