-2
{scenarios: "[{purchasePrice:11,downPayment:1,term:30,rate:1}]"}

That's my object. I want to turn the value of scenarios into the array its trying to be from the string it is.

Is there a quick tool so I don't have to hack it together from .splits?

2
  • 2
    eval() can be used Commented Sep 19, 2016 at 17:32
  • 1
    How is that string created? Commented Sep 19, 2016 at 17:34

1 Answer 1

0

Yes, if the string comes from a trusted source, you can use eval, which will evaluate the string as JavaScript code:

var scenarios = eval(theObject.scenarios);

Since eval allows running any arbitrary code, don't use it on strings from untrusted sources. Additionally, it tends to make it difficult or impossible for the JavaScript engine to optimize the code it's in, so isolate your use of it in a function so the impact is kept to a minimum.

Sign up to request clarification or add additional context in comments.

6 Comments

Even though it's not valid JSON, it's still probably safer to do JSON.parse(obj.scenarios); right?
@Jonathan: Well, no, because that will just fail with an error. If it were valid JSON, then yes, it would be much better to use JSON.parse.
Ah sorry, I see...
Unfortunately it is a querystring param. Is there another way to store an array of objects safely in the QS?
@AronGreenspan Why not use JSON.stringify and JSON.parse
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.