G McLuhan G McLuhan - 25 days ago 5
Javascript Question

How to save my javascript array to a file?

My phonegap/iOS application has a file named data.js which contains an array. The application works with this array and some changes are stored into it.

Now when the user quits my application I want it to save the changes to the data.js file. Is there any way to do this?

UPDATE This is my array (the only thing in data.js):

var data = [
[0, "A", "B", "0", "0"],
[1, "C", "D", "0", "0"],
[2, "E", "F", "0", "0"],
...
];


SOLVED!
I used JSON to stringify my array and save it to the localStorage. It only has to work with Mobile Safari, so this is a good solution. Thanks for giving me the hints that made me solve my problem.

Answer

If you are using phonegap than for sure you can work with file system. Solution is to encode your array into JSON using serializeArray() method in JQuery.

Once you encode your array you will get JSON string which you have to store in a file using PhoneGap's Filewriter() function. For more detail on that visit this link.

I hope it helped you :-).