node.js - Get random line from .txt file then remove the line from .txt file -


need on one. i'm creating discord bot in node.js.

what do:

read random line local .txt file.

then forward discord (i know how)

then delete exact line got, local .txt file.

almost array. but, local one, can fast edit.

i'm still new node , javascript, learn trying , reading, got stuck here, that's why need professional :-)

chart

hope made understand problem, , can me.

i have following:

let fs = require('fs'); let randomnumber =  fs.readfilesync('./resources/array.txt').tostring().split("\n"); let item = randomnumber[math.floor(math.random() * randomnumber.length)]; 

which outputs random number file.

as long file isn't big , isn't being appended during running of script, take data, store in memory, make change array, remove data text file, , rewrite data array file

const fs = require('fs'); // use const ** const data = fs.readfilesync('./array.txt'); const splitdata = data.split('\n'); const randomnumber = math.floor(math.random() * splitdate.length); const line = splitdata.splice(randomnumber, 1); *** fs.writefilesync('./array.txt', splitdata.splice.join('\n'); **** console.log(line); // random line 

** https://developer.mozilla.org/en-us/docs/web/javascript/reference/statements/const

*** https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/splice

**** https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -