Last active
November 24, 2020 10:13
-
-
Save Josh4324/409a995cab81b1e27542cf7b06968cde to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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