Javascript string to variables

Let's say I have a javascript string formatted like:

"var1='hello'&var2='value2'&var3='value3'" - is there a way for me to parse that/evaluate it turn that string into actual variables?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Before we talk code - can you tell us what you want to do with the variables. There are different ways to solve this - if you explain your intention it will help to pinpoint what the right solution is.
erzoolanderAuthor Commented:
I've got an app I'm writing that scans a barcode.  The barcode only can return text/a string - which I'm going to format as variables.  I need something that interprets the returned text - and then parses it out as the appropriate values.
Julian HansenCommented:
You can do something like this
var input = "var1='hello'&var2='value2'&var3='value3'";
var data = parseData(input);

function parseData(inp)
  var data = {};
  inp = inp.split('&');
  for(var i = 0; i < inp.length; i++) {
    var item = inp[i].split('=');
    data[item[0]] = item[1];
  return data;

Open in new window

You can now access your variables like this


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
erzoolanderAuthor Commented:
Perfect!  Gracias!
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.