[Solved] Convert an Array Object that is a String [duplicate]


Your input is structured as JSON. You can thus simply use any JSON parsing method or library. For example JSON.parse(stringArray) (documentation).

Here is a snippet which uses JSON.parse:

var stringArray = "[[1,2,3,4],[5,6,7,8]]";
var parsedArray = JSON.parse(stringArray);

$('#first').html(parsedArray[0].toString());
$('#second').html(parsedArray[1].toString());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
First element: <span id="first"></span><br>
Second element: <span id="second"></span>

You can also use the eval function (documentation) but please note that you should avoid that function at all cost if possible. It is dangerous and often slower than its alternatives. Here is more on that.

However here is a version using the eval method:

var stringArray = "[[1,2,3,4],[5,6,7,8]]";
var parsedArray = eval(stringArray);

$('#first').html(parsedArray[0].toString());
$('#second').html(parsedArray[1].toString());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
First element: <span id="first"></span><br>
Second element: <span id="second"></span>

3

solved Convert an Array Object that is a String [duplicate]