i would like to convert this string:
'[
['Row1 of first array', 'Row2 of first array'],
['Row1 of 2nd array', 'Row2 of 2nd array']
]'
Into an array with three arrays of one dimension and two items.
My expected output is an array with 2 elements:
- Array 1
- Array 2
And every array has two elements inside.
Is there any in Jquery to do this conversion?
That’s not a valid string — you’re nesting single quotes inside of single quotes. However, if you convert the string into one using double quotes on the inside:
then you could simply parse it as a JSON object:
This is far, FAR safer than using
eval, which should only be done when you know EXACTLY what’s inside the string, and even then, it’s a sign of a lazy developer. When usingparseJSON, you know that you’re getting either an object or an array when you’re done — when usingeval, anything might happen.