i would like to remove an object of specified id: I tried by this:
app.delete("/:id", (req, res) => {
const id = req.params.id;
try {
const filtered = games.filter(function (el) {
return el.id !== id;
});
res.send({ rest: filtered });
} catch (error) {
res.send({ error: error.message });
}
});
This although, does not remove the object in games array of objects, but returns the filtered array. Does anyone have an idea on that?
filter does not change the original array, it creates a new array based on the filter you provided.
You could set the games array to the now filtered array, and that would remove the object.
games = games.filter(function (el) {
return el.id !== id;
});
res.send({ rest: games });