0

Dynamic creation of object from string

I have the string {"person":"me" ,"age":"30"} embedded in a text. I extract it from the text using

text.match(/{.*}/);

I want to be able to use the object.keys and object.values methods to get keys to create menus dynamically and return the values based on menu selection.

It appears that I need to be able to create an object dynamically. I would like to do it without having to loop through the string. Is there any function or method that can take the result of text.match as input and give me json that can be used with object.keys and object.values etc.

n4m31ess_c0d3r
  • 3,028
  • 5
  • 26
  • 35
  • 2
    Try: `var result = JSON.parse(string);` – Washington Guedes Jan 04 '18 at 14:17
  • 1
    You don't want to "create json from string." JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. In your case, you're dealing with JSON (in a string) that you want to convert to non-JSON (not a string). You do that, as the linked question's answers say, via `JSON.parse`. – T.J. Crowder Jan 04 '18 at 14:18
  • Okay I want to create data that can by handled by object.keys and object.values. The structure looks like the string I have shown. – user8960345 Jan 04 '18 at 14:46
  • This says closed as duplicate but the proposed solution does not recognize the text.match as an object when This is not solved – user8960345 Jan 04 '18 at 14:55

0 Answers0