Skip to content

Instantly share code, notes, and snippets.

@JefMari
Forked from srph/browser.js
Created July 5, 2016 03:32
Show Gist options
  • Select an option

  • Save JefMari/bfc43a21b26e4f06c8b9bfb200edab03 to your computer and use it in GitHub Desktop.

Select an option

Save JefMari/bfc43a21b26e4f06c8b9bfb200edab03 to your computer and use it in GitHub Desktop.
jeff: tweaking array from storage source
1. get file contents
2. convert to json
3. push item
4. save json to file
// Local Storage
var rows = JSON.parse(localStorage.getItem('names') || []);
rows.push('names');
localStorage.setItem('names', JSON.stringify(rows));
// Node.js
var fs = require('fs');
try {
var rows = fs.readFileSync('names.json', 'utf-8') || {};
} catch(e) {
fs.writeFileSync('names.json', '[]');
rows = [];
}
rows.push('hello');
rows.writeFileSync('names.json', rows);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment