How do I convert a string to a list? [MediaWiki API Help]

I was using the MediaWiki API, and got a successful responseText. However, it’s in a giant string-list. It looks sort of like this (same idea, but without the length):

"['1','2','3']"

How do I convert this strange string into a list?

Codepen: http://codepen.io/AwesomeIndustry/pen/eBgjwX?editors=0011

EDIT: I ended up using

eval("globalResponse = "+globalResponse);

Is there a better way to do this?

There is! It’s not obvious, though. You just have to use a more appropriate query string for the search. Here’s the one I use:

action=query&list=search&srsearch=${searchTerm}&utf8&format=json

That will return an array of results objects. As a warning for the future, don’t use eval(). Like, ever. The only time that it’s not your worst option is when making the calculator project, and even then you have to accept that you’re just lazy (like I was).

I know you shouldn’t use eval(). However, it’s just so tempting sometimes. I mean, it executes a string of code. I think that’s cool. :slight_smile:

Instead of using eval() to parse the string you also could have used JSON.parse().

JSON.parse('[1,2,3]')
> [1, 2, 3]

If the list was just comma separated, without the brackets, I would have used split(). With the only downside being that the individual values are now strings rather than numbers. (Which can always be remedied by using parseInt() or parseFloat().

'1,2,3'.split(',')
> ["1", "2", "3"]
'1,2,3'.split(',').map(string => parseInt(string, 10))
> [1, 2, 3]