-
-
Save JefMari/bfc43a21b26e4f06c8b9bfb200edab03 to your computer and use it in GitHub Desktop.
jeff: tweaking array from storage source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1. get file contents | |
| 2. convert to json | |
| 3. push item | |
| 4. save json to file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Local Storage | |
| var rows = JSON.parse(localStorage.getItem('names') || []); | |
| rows.push('names'); | |
| localStorage.setItem('names', JSON.stringify(rows)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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