gespinha
gespinha

Reputation: 8487

String with Object to Object

I have the following string:

var arr = "[{'one' : '1'}, {'two' : '2'}]";

How can I pass this string to an object?

I've tried the following but it doesn't seem to work:

arr.split(',');

Upvotes: 1

Views: 40

Answers (1)

Saar
Saar

Reputation: 2306

you must first transform single quotes to double quotes then use JSON.parse

JSON.parse(arr.replace(/'/g,'"'))

Upvotes: 4

Related Questions