Project Problem -- to do list

Hello,

I am stuck on removing the to-do list (Line 80). I try to use item.filter() to filter out the item. But it has an error saying the “item. filter() is not a function”. I don’t know how to do it. :sob:

To-Do-List codesandbox

filter() is a function that is attached to Arrays.
Array.prototype.filter() - JavaScript | MDN
In your code item.filter(), item is an element from the newList array.

const item = newList.find(
      (item) => item["key-data"] === e.target.getAttribute("key-data")
    );

So what you want to be doing is filtering out the item you want from newList.
This will do that for you…

  handler = (handlerKey, e) => {
    const { list } = this.state;
    // change newList to `let` so it can be changed later
    let newList = [...list];
    const item = newList.find(
      (item) => item["key-data"] === e.target.getAttribute("key-data")
    );
    switch (handlerKey) {
      case "strikeThru":
        item.isDone = !item.isDone;
        break;
      case "editClick":
        item.isEditing = !item.isEditing;
        break;
      case "editChange":
        item.editValue = e.target.value;
        break;
      case "confirm":
        item.isEditing = !item.isEditing;
        item.text = item.editValue;
        break;
      case "remove":
        // filter the newList so that every item is included except the one who's key-data matches the selected item
        newList = newList.filter(
          (todo) => todo["key-data"] !== item["key-data"]
        );
        // item.text = item.filter(item => item.text !== item["key-data"]);
        break;
      default:
        break;
    }
    this.setState({ list: newList });
  };
1 Like

Ah, I see. Thank you so much Paez!!

2 Likes