first of all, I wanna say that ** I have looked into some of the different topics hovering around with this.** If not 100 and a couple of youtube videos.
I've been sitting stuck on it for the last many hours.
I have a .json file structured such as this:
{
"PlayerData": [
{
"steamId": "76561193436459",
"playerName": "x",
"banReason": "x",
}
]
}
And so forth - you get the idea out from the array.
I've been fiddling with different kinds of Javascript code such as forEach() & (let i > 0 = bannedPlayerData.length i++) types of code, as well as trying out the splice method to cut out an object of this element.
What I am trying to achieve is that depending on the steam id you input - it will remove the entire block/object surrounding to that Steam ID.
So if I in this case input the following for steamid2
765611934365823
it would supposedly remove this entire bit.:
{
"steamId": "765611934365823",
"playerName": "f",
"banReason": "f",
},
However, after trying to use .splice(i, 1) a lot - there is no change at all in my JSON file. Example of a code I most recently tried:
Test: async function (message, server, steamid2) {
try {
const data = fse.readJsonSync(serverSettings + "Bans.json")
for (var i = 0; i < data.bannedPlayerData.length; i++) {
//splice(i, 1)
}
} catch (error) {
console.error(error)
}
},
Any idea of what goes wrong? It gets inside where the console.log is - and it actually doesn't print any errors using the command; there's just no simple change of structure in my JSON file, there are no changes happening.
I've been searching for quite a few hours for npm alternatives to using splice, even the delete... But here we are talking about a file that could have potentially thousands of data (currently its an example of 3) - and I kinda wanna avoid the delete XXX code, due hearing that it leaves white spaces after deleting; which could lead into corrupting my JSON file.
I've done a mix of both answers from Lioness100 & aRvi
Doing
data.bannedPlayerData.splice(data.bannedPlayerData.findIndex((u) => u.steamId === steamid2), 1)
Spliced/split the data as wanted. Saving the file through fse.writeJsonSync
afterwards helped me succeeding in the expected result.
Both answers were posted as comments to my post, and not as a direct answer - hence i am unable to provide them both with a green check mark.