[英]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.