I have a String value as input that contains a 2-dimensional array in the following format:
"[[1,0,1,1],[0,1,0,1],[1,0,1,1],[0,1,0,1]]"
Also, I need to build the actual multidimensional array and store it in a new variable.
var arr = [[1,0,1,1],[0,1,0,1],[1,0,1,1],[0,1,0,1]];
I know I can traverse the String or even use Regular Expressions to build the array. But I wonder if there is any function similar to eval() in Python to convert the String to an equivalent JS array object directly (despite being a slow process).
var arr = eval("[[1,0,1,1],[0,1,0,1],[1,0,1,1],[0,1,0,1]]");
eval(in JavaScript) is generally not recommended unless you have absolute trust that the string you'reevaling won't contain malicious code, since it actually runs code. Soeval("maliciousCodeToWipeYourStorage()")will run that malicious code. But that string is also valid JSON, so you could useJSON.parseinstead.