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
};
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
};
You could use the eval
function as follows:
eval("var obj = {" + yourString + "}");
console.log(obj);