尝试在 Javascript 中加入二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2016143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:25:00  来源:igfitidea点击:

Trying to join a two-dimensional array in Javascript

javascriptarraysmultidimensional-arrayimplode

提问by Arda Xi

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:

我正在尝试将二维数组转换为字符串,以便将其存储在 localStorage 数组中。但是,我无法识别此代码有问题:

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.

至于有什么问题,多维数组我的意思是array[0][1]等。当输入到localStorage时,所有的'string'都被简化为@,这意味着@的另一边还有数组。

采纳答案by Roland Bouman

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 arrayis indeed an array, you should not use a for..in loop. That's for objects. Just use a

至少,如果array确实是一个数组,则不应使用for..in loop. 那是针对对象的。只需使用一个

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

回答by Jonatas Walker

Nowadays this is as simple as:

现在这很简单:

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

回答by user1357172

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

JSON现在是现代浏览器的标准。您可以使用它来“串化”(转换为 JSON 字符串)和从 JSON 字符串“解析”转换。

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

您可以使用该JSON.stringify函数将二维数组转换为 JSON 并将其粘贴到localStorage. 然后您可以使用JSON.parse将其转换回数组。

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

var backToOriginal = JSON.parse(localStorage[key]);

回答by Mark Byers

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

Javascript 没有二维数组。它只有参差不齐的阵列。您的代码适用于我的适当输入:

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?

你能告诉我们你正在使用什么输入以及你得到什么输出吗?

回答by James Maroney

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

你的代码对我来说似乎工作正常,在 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);