0

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?

1 Answer 1

3

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 });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.