1

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"])); I get an output of "'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.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
simplexshotz
  • 139
  • 12
  • 7
    Have you considered, or are you aware of, [`JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) or [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)? – David Thomas Aug 25 '18 at 12:44
  • I am now! Thanks for that. – simplexshotz Aug 25 '18 at 12:55
  • No problem at all! (I do applaud you for attempting your own solution though, but until you create your own API for the functionality you're likely to encounter a lot of edge-cases.) – David Thomas Aug 25 '18 at 12:57

4 Answers4

3

Instead of doing your own bespoke solution, unless you have something that can't be represented in JSON (your example can be), use 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));
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

As David said there's 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));
}
aymen ayoo
  • 92
  • 9
0

It can be achieved with help of JSON.stringify and JSON.parse functions. 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
Monil
  • 11
  • 1
  • I would like to know the reason of downvote. I think I have given right answer with reference to question title of converting the array to string and then string back to the array. I have also tested above code with reference to array to string and then back to array conversation. I would appreciate for valid reason of downvote. Thanks. – Monil Aug 27 '18 at 04:10
0

The other answers have already covered it, but note that P5.js also provides functionality for working, saving, and loading JSON directly.

Take a look at the saveJSON() and loadJSON() functions in the reference.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107