0

I've got a file made using JavaScript that contains the contents of the localstorage for a website. It's been stringified and is (now) separated by "|". I want to split this string after every "|" into a separate string so I can 1 by 1 input each variable with its value into localstorage, as, from what I know, you can't set multiple items at once. Here's what I've got so far but after researching I've yet to find a solution other than putting into an array...

<input type="file" id="file" accept=".io">
<div id="contents"></div>
<script>
var X;
document.getElementById('file').addEventListener('change',
    function () {
        var fr = new FileReader();
        fr.onload = function () {
            document.getElementById('contents').textContent = 
                this.result.replace(/,/g,"|").replace(/:/g,",").slice(1,-1);
        };
        fr.readAsText(this.files[0]);
    }
);
localStorage.setItem("test","1223asd3423");
</script>

I have a feeling that what I'm doing is an elongated and inefficient way to export/import localstorage data and there's some sort of solution containing an amalgam of for loops and other forms of JavaScript I'm yet to branch into as a novice.

Here is an example of how the raw file is formatted just for reference:

Settings.io: (.io file extension, not website domain)

{"AB12_Rubicon":"37","AB9":"99","backgroundColor":"#2e2e2e",....}

The new string

"AB12_Rubicon","37"|"AB9","99"|"backgroundColor","#2e2e2e"|....

example of desired result

Newstring1 = '"AB12_Rubicon","37"'; Newstring2 = '"AB9","99"';

  • 1
    give us a an example string and the wanted result – Hussein Nazzal Mar 27 '16 at 21:00
  • Newstring1 ="AB12_Rubicon":"37" Newstring2 = "AB9":"99" –  Mar 27 '16 at 21:06
  • I don't see a | delimiter in that string. – Andy Mar 27 '16 at 21:08
  • i replaced the ":"s with ","s in the hope of being able to do something like localStorage.setItem(newstring1) –  Mar 27 '16 at 21:08
  • the string given is an extract of the raw file, before charcter replacement. –  Mar 27 '16 at 21:08
  • Maybe you want to look at [this SO question and answer](http://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage). – Bikonja Mar 27 '16 at 21:10
  • 2
    your question states that there is a string with a pipe character delimiter and yet the example you provide for us has no pipe characters what so ever. i think you should take a moment and think about what you need and refine your question with an appropriate example of the input string and the output that your looking for . – Hussein Nazzal Mar 27 '16 at 21:25

1 Answers1

0

im sure i'll be editing this answer but what ever

if what you are getting is this input

{"AB12_Rubicon":"37","AB9":"99","backgroundColor":"#2e2e2e",....}

then it is not a string it is JSON you dont have to split it to get the values. you have to loop though it .

first you need to check if the JSON is stringified if it is then you need to convert it back to JSON

to check simply use console.log(typeof input); if it is a string use JSON.parse(input);

now assuming you have the object situation all sorted out

here is the code to extract the values

for (var key in input){
    var current_key = key; // will get value before the ":"
    var current_value = input[key]; // value after ":"
    // do what ever you need
}

edit :

just because the question title is so vage some ne might come across this question looking ofr string minupulation us myvariable_with_string.split(',')

Hussein Nazzal
  • 2,557
  • 18
  • 35