node js store add values in file
let data = JSON.stringify(student);
fs.writeFileSync('student-2.json', data);const fs = require('fs');
let obj = {
table: []
};
fs.exists('myjsonfile.json', function(exists) {
if (exists) {
fs.readFile('myjsonfile.json', function readFileCallback(err, data) {
if (err) {
console.log(err);
} else {
obj = JSON.parse(data);
//Data that you want add to the file
for (i = 0; i < 5; i++) {
obj.table.push({
id: i,
square: i * i
});
}
let json = JSON.stringify(obj);
fs.writeFile('myjsonfile.json', json);
}
});
} else {
console.log("file not exists");
//Data that you want add to the file
for (i = 0; i < 5; i++) {
obj.table.push({
id: i,
square: i * i
});
}
let json = JSON.stringify(obj);
fs.writeFile('myjsonfile.json', json);
}
});
Also in JavaScript:
- how to trim the file name when length more than 10 in angular
- convert json to 2d array
- javascript create svg
- datatables hide showing entries
- jquery check if screen size
- create a html table dynamically using javascript
- javascript Prevent Object MutationPassed
- get value from textbox in vanilla javascript
- download comma separated file, react
- axios js and react
- date object javascript
- check if element is visible jquery
- check if object has method javascript
- clear terminal node js
- scroll to top in react
- json enconde
- how to clear terminal in vs code
- checkbox default value and checked value get in jquery
- node js code for saving first middle and last name
- angular component lifecycle hooks
- javascrip check if string contains substring
- require is not defined javascript
- testing library react hooks
- javascript set class