Q:

How do I save an array to a file and manipulate it from within my code?

This is in p5.js which includes most javascript functions!
I am trying to make a save-file for my game. By this I mean: the user presses the save button in my game. It updates an array that is saved on a file included in the game package, the player keeps playing. How would I do something like this (creating files that can be accessed by my code and changed).
    var SM = {
//save files
    sf1: [1,0,0,0,0],
         [0,0,0,0,0],
         [0,0,0,0,0],

    sf2: [1,0,0,0,0],
         [0,0,0,0,0],
         [0,0,0,0,0],

    sf3: [1,0,0,0,0],
         [0,0,0,0,0],
         [0,0,0,0,0],
};

One more thing (FOR PROCESSING CODERS FROM HERE ON): I tried to use processing functions like saveStrings(); and loadStrings(); but I couldn't get saveStrings() to save to a specific location nor could I properly load a txt file. Here is the code I used for that:
var result;
function preload() {
  result = loadStrings('assets/nouns.txt');
}

function setup() {
  background(200);
  var ind = floor(random(result.length));
  text(result[ind], 10, 10, 80, 80);
}

I had a folder called assets within the sketch folder and assets had a txt file called nouns with strings in it (downloaded from saveStrings then manually moved) but the sketch wont go past the loading screen?

A:

If you are running it from a browser, you can't save or load a file how you want, period. Saving and loading files in browser JavaScript involves user interaction, and they get to pick the file and where it saves.
If you want to save it locally, instead of trying to write it to a file, you should write and read it from localStorage, which you can then do just fine.
// save
localStorage.setItem('saveData', data);

// load
const data = localStorage.getItem('saveData');

If it is somehow a game run directly on the client (out of the browser), like written in Node.js, then you'd want to use the fs functions.

To expand a bit, if you have your save data as an object:
const saveData = {
  state: [1,2,3],
  name: 'player'
};

Then to save it, you would simply call:
localStorage.setItem('saveData', JSON.stringify(data));

You'll want to stringify it when you save it to make it work properly. To read it back, you can then just read it back with getItem():
const data = JSON.parse(localStorage.getItem('saveData') || '{}');

(That extra || '{}' bit will handle if it hasn't been saved before and give you an empty object.)
It's actually much easier than trying to write a JavaScript file that you would then read in. Even if you were writing a file, you'd probably want to write it as JSON, not JavaScript.

