Trying to join a two-dimensional array in Javascript
Asked Answered
V

5

14

I'm trying to convert a two-dimensional array to a string in order to store it in the localStorage array. However, there is something wrong with this code I cannot identify:

for(x in array) {
    if(array[x] instanceof Array) {
        array[x] = array[x].join("`");
    }
}
var string = array.join("@");
localStorage[key] = string;

Does anyone have an idea what I'm doing wrong?

As for what's wrong, by multidimensional array I mean array[0][1] etc. When input into localStorage, all the 'string' is reduced to is @, implying on the other side of the @ there are still arrays.

Versieversification answered 6/1, 2010 at 20:52 Comment(3)
what is the something that is wrong? surely, you ucan say what your input is, what you expected, and what the undesired output is?Ire
On a sidenote, you might want to consider using JSON.stringify instead of custom seperators for your purpos.Biogen
@Thomas - At the time of writing, JSON.stringify was not available in all the browsers (specifically, not in the stable build of Chrome). Now, I would use that, yes.Versieversification
I
5

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("`");
    }
}
Ire answered 6/1, 2010 at 21:1 Comment(1)
for..in loops are ok to use for Arrays, it's not only for objects..as long as you use 'hasOwnProperty'.Defoliant
O
32

Nowadays this is as simple as:

[[1,2],[3,4]].map(e => e.join(':')).join(';'); // 1:2;3:4
Obedient answered 2/12, 2016 at 11:6 Comment(0)
I
5

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("`");
    }
}
Ire answered 6/1, 2010 at 21:1 Comment(1)
for..in loops are ok to use for Arrays, it's not only for objects..as long as you use 'hasOwnProperty'.Defoliant
D
2

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]);
Duteous answered 27/2, 2015 at 23:32 Comment(1)
Back in January 2010, JSON wasn't available as such in stable browsers. ;)Versieversification
H
1

Javascript doesn't have two dimensional arrays. It has only ragged arrays. Your code works for me for an appropriate input:

array = [[1,2],[3,4]];
for(x in array) {
    if(array[x] instanceof Array) {
        array[x] = array[x].join("`");
    }
}
var string = array.join("@");
alert(string);

Output:

1`2@3`4

Could you show us what input you are using and what output you get?

Hypostasize answered 6/1, 2010 at 20:59 Comment(0)
R
0

Your code seems to work fine for me, testing in Firefox.

Is it failing in a specific browser?

var array = [
["a","b"],
["c","d","e"]];
for(x in array) {
    if(array[x] instanceof Array) {
        array[x] = array[x].join("`");
    }
}
var string = array.join("@");
console.log(string);
Rodney answered 6/1, 2010 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.