Basically what I'm trying to do is, from the following string:
--metodo: youtube --busqueda: best music mix
Get the "method", which should return youtube
, and the "search", which should returnbest music mix
As if it were specifying parameters when executing a command.
So the expected result would be an object with the following info:
{
metodo: "youtube",
busqueda: "best music mix"
}
I hope you can help me to do this in the most automatic way possible (without using .replace().. etc). Thank you.
It occurs to me that you could do something like this
For the sake of code clarity I have not done any checks and have assumed that the input string str is well formed and contains no errors. I have used a forEach loop just to allow an unspecified number of parameters.
The operation is simple, first I divide the input string into substrings taking the double hyphen as separator. Then I divide each substring using the colon as a separator, the part on the left will be the name of the parameter and the one on the right its value. I add these items to an object that will be the desired output. I hope it is understood. All the best.