简体   繁体   中英

How can i remove an object from array of objects in app.delete using express

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 });

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