Skip to content

Instantly share code, notes, and snippets.

@srph
Last active July 5, 2016 03:32
Show Gist options
  • Select an option

  • Save srph/dee70279b08d5845ade407958cb3a0a0 to your computer and use it in GitHub Desktop.

Select an option

Save srph/dee70279b08d5845ade407958cb3a0a0 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