Skip to content

Instantly share code, notes, and snippets.

@Josh4324
Last active November 24, 2020 10:13
Show Gist options
  • Save Josh4324/409a995cab81b1e27542cf7b06968cde to your computer and use it in GitHub Desktop.
Save Josh4324/409a995cab81b1e27542cf7b06968cde to your computer and use it in GitHub Desktop.
const deleteTodo = (title) => {
try {
// read from the todos.json if it exists
const todoBuffer = fs.readFileSync("todos.json");
// convert it to string
let dataJSON = todoBuffer.toString();
// parse the data
const todos = JSON.parse(dataJSON);
const remain = todos.filter((item) => {
return item.title != title;
})
dataJSON = JSON.stringify(remain);
fs.writeFileSync("todos.json", dataJSON);
if (remain.length === todos.length){
console.log("This file does not exist")
}else{
console.log("Todo was deleted succesfully")
}
}catch(error){
console.log("An error just occured")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment