Convert String to fix

1

I have a String like the following:

var sArray="[0, [6, 2], null, 7, 1]";

However, I want to convert them to a fix type, because when I do it in the following way:

for (var i = 0; i < arr.length; i+=1) {
    console.log(arr[i]);
}

It prints me character by character of the String.

    
asked by gibran alexis moreno zuñiga 10.11.2018 в 01:54
source

2 answers

2

Interestingly [0, [6, 2], null, 7, 1] is json valid so you could use JSON.parse and just that:

console.log(JSON.parse("[0, [6, 2], null, 7, 1]"))
    
answered by 10.11.2018 / 07:04
source
0

This way you could do it:

var sArray="[0, [6, 2], null, 7, 1]";
var nuevo = JSON.parse(sArray);
var newarray = [];

    $.each(nuevo, function(i, val) {
        newarray.push(val);
     });

alert(newarray);
    
answered by 10.11.2018 в 02:07