If the string is certain to be secure, the simplest would be to concatenat [ and ] to the beginning and end, and then eval it.

var arr = eval("[" + myString + "]");

If you wanted greater safety, use double quotes for your strings, and use JSON.parse() in the same way.

var myString = '["Item", "Count"],["iPad",2],["Android",1]';

var arr = JSON.parse("[" + myString + "]");

This will limit you to supported JSON data types, but given your example string, it'll work fine.

Answer from I Hate Lazy on Stack Overflow
🌐
freeCodeCamp
forum.freecodecamp.org β€Ί t β€Ί how-to-convert-string-to-a-2-dim-array-in-js β€Ί 237435
How to convert string to a 2-dim array in js? - The freeCodeCamp Forum
November 1, 2018 - I want to convert a string: TWENTY,60,TEN,20 in to this array: [["TWENTY", 60], ["TEN", 20]] The string is saved in a variable, which is declared this way: var result = {status:"", change:[]}; and values are pushed in it this way: result.status = "OPEN"; result.change.push("TWENTY"); result.change.push(60); result.change.push("TEN"); result.change.push(20); As an overall goal, it is required to return the value this way: {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20]]} How to do...
Discussions

JavaScript: How to convert a string to a 2D array - Stack Overflow
But that string is also valid JSON, so you could use JSON.parse instead. T.J. Crowder – T.J. Crowder Β· 2022-10-30 10:05:04 +00:00 Commented Oct 30, 2022 at 10:05 ... Note: This isn't a Python question, so I've removed the Python tags. Just because you're referring to Python, it doesn't mean that Python tag followers need to have this JavaScript ... More on stackoverflow.com
🌐 stackoverflow.com
convert a string into a two-dimensional array in javascript - Stack Overflow
I'm trying to convert a string like this "10|15|1,hi,0,-1,bye,2" where the first two elements 10|15 mean something different than 1,hi,0,-1,bye,2. I would like to separate them from each other. A n... More on stackoverflow.com
🌐 stackoverflow.com
javascript - How to convert string into 2-dimension array? - Stack Overflow
In order to convert string into 1-dimensional JavaScript array I could use 'eval()'. But how to convert string into 2-dimensional array? My string is: ['stage 1', 1, 11, 111],['Stage 2', 2, 22, 2... More on stackoverflow.com
🌐 stackoverflow.com
Javascript: split string into 2d array - Stack Overflow
I have a string of month and years: var months= "2010_1,2010_3,2011_4,2011_7"; I want to make this into a 2d array with the year in the first position of each array and the month in the second pos... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Lage
lage.us β€Ί Javascript-Split-String-Into-2-d-Array.html
Split a String Into a 2-Dimensional-Array - Lage.us
The first delimiter is for the ...n(x){return x.split(d2)}); } javascript var string "this,is,the,first,line. This is the second line." array2d = stringTo2dArray(string, "....
🌐
W3Resource
w3resource.com β€Ί javascript-exercises β€Ί fundamental β€Ί javascript-fundamental-exercise-3.php
JavaScript fundamental (ES6 Syntax): Converts a comma-separated values string to a 2D array - w3resource
July 3, 2025 - data .slice(omitFirstRow ? data.indexOf('\n') + 1 : 0) .split('\n') .map(v => v.split(delimiter)); // Test cases console.log(csv_to_array('a,b\nc,d')); // [['a', 'b'], ['c', 'd']] console.log(csv_to_array('a;b\nc;d', ';')); // [['a', 'b'], ['c', 'd']] console.log(csv_to_array('head1,head2\na,b\nc,d', ',', true)); // [['a', 'b'], ['c', 'd']] ... See the Pen javascript-basic-exercise-1-3 by w3resource (@w3resource) on CodePen. ... Write a JavaScript program that converts a CSV string into a two-dimensional array, correctly handling quoted commas.
Find elsewhere
🌐
CodingTechRoom
codingtechroom.com β€Ί question β€Ί -convert-string-to-2d-array-js
How to Convert a String into a Two-Dimensional Array in JavaScript? - CodingTechRoom
Use the 'split()' method to divide the string into parts based on a primary delimiter. Map over the resulting array and use 'split()' again to divide each string into a secondary array using a different delimiter.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί javascript β€Ί how-to-convert-csv-string-file-to-a-2d-array-of-objects-using-javascript
How to convert CSV string file to a 2D array of objects using JavaScript ? - GeeksforGeeks
July 23, 2025 - The JavaScript string slice() method extracts parts of a string and returns the extracted parts in a new string taking '\n' as the first occurrence. Data Values are stored using "\n" as the delimiter. JavaScript map() function will iterate over all values of the title values array and append each object at the end of the array Β· The "storeKeyValue" variable is used to store each key with its respective values. Example: In this example, we will convert a comma-separated value (CSV) to a javascript array using the slice(), map(), split(), and reduce() methods of Javascript.
🌐
DEV Community
dev.to β€Ί sanderdebr β€Ί converting-csv-string-to-a-2d-array-of-objects-in-es6-2hm2
Converting CSV string to a 2D array of objects in ES6 - DEV Community
March 1, 2020 - In this article I will explain how to convert a comma-separated values (CSV) string to a 2D array of object using Javascript.
🌐
Reddit
reddit.com β€Ί r/javahelp β€Ί how to turn a string of a 2d array into a 2d array?
r/javahelp on Reddit: How to turn a string of a 2D array into a 2D array?
February 25, 2024 -

Hello

I have an input of a 2D array in the form of a string. And I want to turn it into a 2D array and store it.

I have tried using .replace() but it’s not working as I expected.

An example of an input is

String x = β€œ{ {F, 40 , 40 , 2000},{L, 60 , 60 , 1000},{F, 40 , 40 , 2000}}”

And I want to turn it into an array like

String [][] y = { {β€œF” , β€œ40” , β€œ40” , β€œ2000”}, {β€œB” ,β€œ60” , β€œ60” , β€œ1000”}, {β€œF” , β€œ40” , β€œ40” , β€œ2000”}}

I saw some tips on using replace() and split() but I am unsure how to use them to achieve what I want or if I need another function to solve this.

Any tips to help solve this would be appreciated.

Top answer
1 of 5
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2 of 5
1
This is not gonna help you if you are doing a homework. But for real-world usage, here's how such parsing can be done with Google Mug ( example code . recommended internally at Google too): String x = "{ {F, 40 , 40 , 2000},{L, 60 , 60 , 1000},{F, 40 , 40 , 2000}}"; String nested = Substring.between(first('{'), last('}')).from(x).get(); System.out.println( Substring.between('{', '}') .repeatedly() .from(nested) // => [{F, ...}, {L, ...}, {F, ...}] .map(Substring.first(',').repeatedly()::splitThenTrim) .map(items -> items.map(Object::toString).toList()) .toList()); I used toList() to get toString(). But you can change the first toList() to .toArray(String[]::new) and the second to .toArray(String[][]::new). That'll get you the 2D array. Using List is usually preferred over arrays though.
🌐
Medium
medium.com β€Ί @sanderdebr β€Ί converting-csv-to-a-2d-array-of-objects-94d43c56b12d
Converting CSV string to a 2D array of objects in ES6 | by Sander de Bruijn | Medium
February 15, 2020 - Converting CSV string to a 2D array of objects in ES6 In this article I will explain how to convert a comma-separated values (CSV) string to a 2D array of object using Javascript. Let’s say you …