简体   繁体   中英

How to delete something from an Array using GET

The code below is supposed to delete something from an Array from a JSON file by using GET. However, no matter what number is inputted it just goes to the else statement displaying the error message. I can't seem to understand what I'm doing wrong as no errors are showing in the console either.

The objective of this code is to delete the element from the array which is the same as the ID of the element in the JSON file. So if the get request is delete/02 it should delete the element with the Id 02 in the array, not the third element in the array.

 var fs = require('fs'); var rawData = fs.readFileSync('toDoList.json'); var tasksArray = JSON.parse(rawData); console.log(list); app.get('/delete/:id', function (req, res) { for (var i = 0; i < tasksArray.length; i++) { if(tasksArray[i]["id"] == req.params['id']) { tasksArray.splice([i], 1); var json = JSON.stringify(tasksArray); fs.writeFileSync("toDoList.json", json, "utf8"); res.json(tasksArray); console.log[i]; } else { res = res.status(500); res.json({"status": "error"}); } break; } }); 

Here is the JSON if it is any help:

 [ { "id": "01", "desc": "Assignment 1" }, { "id": "02", "desc": "Assignment 2" }, { "id": "03", "desc": "Assignment 3" }, { "id": "04", "desc": "Assignment 4" }, { "id": "05", "desc": "Assignment 5" } ] 

I would prefer a basic filter:

 var tasksArray = [ { "id": "01", "desc": "Assignment 1" }, { "id": "02", "desc": "Assignment 2" }, { "id": "03", "desc": "Assignment 3" }, { "id": "04", "desc": "Assignment 4" }, { "id": "05", "desc": "Assignment 5" } ]; var id_to_delete = "02"; var remainingTasks = tasksArray.filter( task => task.id !== id_to_delete ); // write remainingTasks back into the JSON file as before. console.log( remainingTasks ); 

As per the comment .splice( i, 1 ); should work as well.

PS: I would prefer id to be an actual number instead of a string. That will help with sorting and you do not have to check if you need to prepend 0s when adding an item, just the current length + 1 equals the next id.

 //var fs = require('fs'); //var rawData = fs.readFileSync('toDoList.json'); //var tasksArray = JSON.parse(rawData); var tasksArray = [ { "id": "01", "task": "do 1" }, { "id": "02", "task": "do 2" }, { "id": "03", "task": "do 3" }, { "id": "04", "task": "do 4" }, { "id": "05", "task": "do 5" } ]; console.log( tasksArray ); //app.get('/delete/:id', function (req, res) { //var id_to_delete = req.params.id; var id_to_delete = "02"; var remainingTasks = tasksArray.filter( task => { if ( task.id === id_to_delete ) { console.log( 'filtering away task with id: ' + task.id ); console.log( task ); return false; } else return true; }); // Doublecheck that exactly one item was removed. if ( remainingTasks.length === tasksArray.length - 1 ) { var json = JSON.stringify( remainingTasks ); // fs.writeFileSync("toDoList.json", json, "utf8"); // res.json( remainingTasks ); console.log( json ); } else { //res = res.status(500); //res.json({"status": "error"}); console.log( 'error' ); } // }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM