Trying to join a two-dimensional array in Javascript Trying to join a two-dimensional array in Javascript arrays arrays

Trying to join a two-dimensional array in Javascript


Nowadays this is as simple as:

[[1,2],[3,4]].map(e => e.join(':')).join(';'); // 1:2;3:4


what is the something that is wrong? surely, you ucan say what your input is, what you expected, and what the undesired output is?

At least, if array is indeed an array, you should not use a for..in loop. That's for objects. Just use a

for (var i=0, l=array.length; i<l; i++){    if (array[i] instanceof Array){        array[i] = array[i].join("`");    }}


JSON is now standard in modern browsers. You can use it to "stringify" (convert to a JSON string) and "parse" convert from a JSON string.

You can use the JSON.stringify function to convert your 2D array to JSON and stick it in localStorage. Then you can use JSON.parse to convert it back to an array.

var my2DArray = [[1, 2, 3], [4, 5, 6]];var stringified = JSON.stringify(my2DArray);localStorage[key] = stringified;var backToOriginal = JSON.parse(localStorage[key]);