0

Is there an easy way to take a string like the one below into an array without writing some kind of complicated parsing function?

const stringArray = '[{key1: 'value1', key2: 'value 2'}, {key1: 'value3', key2: 'value4'}, {key1: 'value5', key2: 'value6'}]';

I want to be able to do something like:

const arr = <stringArray CONVERTED TO ARRAY>;
console.log(arr[1].key1); // should log 'value3'

If context helps, I'm writing a utility program in JavaScript that involves grabbing and parsing an array input into a <textarea> by the user.

4
  • 1
    If that mysterious format would be valid JSON, you could JSON.parse it easily Commented Jul 14, 2018 at 16:38
  • 3
    why not take a JSON (ECMA-404) standard compliant string and parse it instead of some fancy lookalike. Commented Jul 14, 2018 at 16:38
  • 1
    The inner quotes should be double quotes though. The string isn't even valid syntax as-is. Commented Jul 14, 2018 at 16:39
  • Possible duplicate of How do I parse this invalid JSON string in jQuery? Commented Jul 14, 2018 at 19:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.