简体   繁体   English

如何将转换后的字符串转换回数组?

[英]How to convert a converted string back into an array?

As far as I know, you can only save strings to local storage. 据我所知,您只能将字符串保存到本地存储中。 So, I had to write a function so that I could save arrays. 因此,我必须编写一个函数才能保存数组。 If I call console.log(fixA(["string1", [5, [false]], "string2"])); 如果我调用console.log(fixA(["string1", [5, [false]], "string2"])); I get an output of "'string1',[5,[false]],'string2'" . 我得到了"'string1',[5,[false]],'string2'" Here it is: 这里是:

 function fixA(array) { var toreturn = ""; for (var i = 0; i < array.length; i++) { if (typeof array[i] === 'object') { toreturn += "[" + fixA(array[i]) + "]"; } else { if (typeof array[i] === 'string') { toreturn += "'" + array[i] + "'"; } else { toreturn += array[i]; } } if (i < array.length - 1) { toreturn += ","; } } return toreturn; } console.log(fixA(["string1", [5, [false]], "string2"])); 

The issue now is that I have no clue how to convert it back. 现在的问题是,我不知道如何将其转换回去。 I've tried a few things but have always gotten stuck on how I convert the arrays back. 我已经尝试了一些方法,但是始终无法转换阵列。 This is basically what I've tried: 这基本上是我尝试过的:

 function fixS(string) { var toreturn = []; var temp = string.split(","); for (var i = 0; i < temp.length; i++) { // I could run a check here to see if temp[i] starts with "[", but I'm not sure how to tell where the array ends. // If it is an array, then I'd need to pass everything inside of the array back into fixS, making it recursive. // The times I tried to do those two things above, I ran into the issue that the commas inside of the sub arrays also split everything, which I don't want (as the recursive function will deal with that). toreturn.push(temp[i]); } return toreturn; } console.log(fixS("'string1',[5,[false]],'string2'")); // This also doesn't return numbers as numbers or booleans as booleans. 

Not much there, but it's as far as I've gotten. 那里不多,但据我所知。 Any help is appreciated. 任何帮助表示赞赏。

Instead of doing your own bespoke solution, unless you have something that can't be represented in JSON (your example can be), use JSON: 除非您有无法用JSON表示的内容(您的示例可以),否则不要使用您自己的定制解决方案,请使用JSON:

On page load: 页面加载:

var data = JSON.parse(localStorage.getItem("data") || "null");
if (!data) {
    // There wasn't any, initialize
}

or 要么

var data = JSON.parse(localStorage.getItem("data") || "{}");

...if you want a blank object if there is nothing in local storage. ...如果本地存储中没有任何内容,则需要空白对象。

When saving your data: 保存数据时:

localStorage.setItem("data", JSON.stringify(data));

As David said there's JSON.stringify() & JSON.parse() ; 正如David所说的,有JSON.stringify()JSON.parse()

you can use those methods : 您可以使用这些方法:

function save_to_storage(id, anything){
    localStorage.setItem(id, JSON.stringify(anything));
}
function load_from_storage(id){
    return JSON.parse(localStorage.getItem(id));
}

It can be achieved with help of JSON.stringify and JSON.parse functions. 可以借助JSON.stringify和JSON.parse函数来实现。 Let me explore with help of code snippet. 让我借助代码片段进行探索。

var original_arr = ["string1", [5, [false]], "string2"];  // array
// Converting this array into string
var converted_str = JSON.stringify(original_arr); // converted into string
// Converting this string back to array
var new_arr = JSON.parse(converted_str );  // back to array

The other answers have already covered it, but note that P5.js also provides functionality for working, saving, and loading JSON directly. 其他答案已经涵盖了它,但是请注意,P5.js还提供了直接工作,保存和加载JSON的功能。

Take a look at the saveJSON() and loadJSON() functions in the reference . 看看在saveJSON()loadJSON()函数的引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数组转换为字符串,再转换回数组 JavaScript 错误 - convert an Array, converted to a String, back to an Array JavaScript Error 如何将使用javascript中的toLocaleString()转换的字符串转换回Date对象? - How can I convert a string converted using toLocaleString() in javascript back to Date Object? 如何使用 javascript 将字符串字段数组转换回数组 - How to convert string field array back into an array with javascript 如何将数组的字符串表示形式转换回数组? - How do you convert a string representation of array back to array? 如何获得此字符串以转换回数组? - How can I get this string to convert back into the array? 如何将十六进制字符串转换为 Uint8Array 并返回 JavaScript? - How to convert a hexadecimal string to Uint8Array and back in JavaScript? 如何将此类示例 object 数组转换为字符串并将其还原? - How to convert such example object array to string and revert it back? json正在转换,如何使用ascii文本转换回对象 - json being converted, how to convert back to object with ascii text 将字符串数组表示形式转换回数组 - Convert string array representation back to an array 如何将 Uint8Array 的对象转换为字符串并将字符串转换回相同的对象? - How to convert the object of Uint8Array to string and convert back string to same object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM