Skip to content
Advertisement

Unable to remove data from json file on disk

I’m unable to find a way to remove whole line of JSON data(line) after it’s used. For some reason delete is not working or rather said not doing anything.

.JSON

JavaScript

code

JavaScript

The goal here is to select random code, which is done but then I need to remove it from .JSON file so it won’t repeat. I tried several things but it’s not working, delete.randomcode etc… the line never removed from the .JSON file.

Advertisement

Answer

Use Array.prototype.splice(index, deleteCount) instead of delete.
delete, on an Array, will just null the key, without removing it.

Save back your modified data using JSON.stringify(mycodes) to that same file.

JavaScript

If you already have that Object out of your Array, and since Objects are passed by reference (like pointer in memory), make use of the Array.prototype.indexOf(someObject) like:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement