Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
If I am given properties as a string in multiple test cases...
var testCase = "Key1: 1, Key2: 2, key3: 3, key4: 4"
...is there a way in JavaScript to convert it to an object?
var myObj = { key1: 1, key2: 2, key3: 3, key4: 4 };
/(.+?): (.+?),?/g
You could use the eval function as follows:
eval
eval("var obj = {" + yourString + "}"); console.log(obj);
Add a comment
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
/(.+?): (.+?),?/g