这个过滤函数为什么不起作用?

huangapple go评论109阅读模式
英文:

Can someone tell me why this filter function doesn't work?

问题

能有人请解释一下为什么这个筛选器不起作用吗?在“朋友卡片”上点击删除按钮应该重新打印出列表,减去了已删除的卡片。然而,虽然警报出现了,但卡片列表并没有改变。问题出在哪里?

编辑:根据评论调整了代码,但仍然没有改变任何东西
编辑2:等等,不对,第一次就对了。意识到 x.index 没有意义,因为我没有索引属性

export default function App() {
  const [friendsState, setFriendsState] = useState(friends);

  function deleteCard(index) {
    alert("删除");
    let newFriends = friendsState.filter((x) => {
      return x.id != index;
    });

    setFriendsState(newFriends);
  }
  
  return (
    <>
      {friendsState.map((x, index) => (
        <Card
          name={x.name}
          home={x.home}
          id={index}
          deleteCard={() => deleteCard(index)}
          key={index}
        />
      ))}
    </>
  );
}
英文:

Can someone please explain me why this filter doesn't work? Clicking the delete button on a "friend card" is supposed to reprint list minus the deleted card. However, while the alert shows up, the list of cards doesn't change. What's the deal?

edit: adjusted code as per comment, but it still doesn't change anything
edit2: wait, no, right the first time. Realized that x.index wouldn't make sense because I have no index property

   export default function App() {
  const [friendsState, setFriendsState] = useState(friends);

  function deleteCard(index) {
    alert(&quot;Deleting&quot;);
    let newFriends = friendsState.filter((x) =&gt; {
      return x.id != index;
    });

    setFriendsState(newFriends);
  }
  
  return (
    &lt;&gt;
      {friendsState.map((x, index) =&gt; (
        &lt;Card
          name={x.name}
          home={x.home}
          id={index}
          deleteCard={() =&gt; deleteCard(index)}
          key={index}
        /&gt;
      ))}
    &lt;/&gt;
  );
}

答案1

得分: 2

Looking at your code, I don't really see a reason you can't just compare the object directly instead of checking properties. Try this instead and see if that works:

export default function App() {
  const [friendsState, setFriendsState] = useState(friends);

  function deleteCard(cardToDelete) {
    alert("Deleting");
    let newFriends = friendsState.filter((x) => {
      return x !== cardToDelete;
    });

    setFriendsState(newFriends);
  }
  
  return (
    <>
      {friendsState.map((x, index) => (
        <Card
          name={x.name}
          home={x.home}
          id={index}
          deleteCard={() => deleteCard(x)}
          key={index}
        />
      ))}
    </>
  );
}

It's pretty clear that your issue is that the id property on the objects do not correspond to the objects' index in the array.

英文:

Looking at your code, I don't really see a reason you can't just compare the object directly instead of checking properties. Try this instead and see if that works:

export default function App() {
  const [friendsState, setFriendsState] = useState(friends);

  function deleteCard(cardToDelete) {
    alert(&quot;Deleting&quot;);
    let newFriends = friendsState.filter((x) =&gt; {
      return x !== cardToDelete;
    });

    setFriendsState(newFriends);
  }
  
  return (
    &lt;&gt;
      {friendsState.map((x, index) =&gt; (
        &lt;Card
          name={x.name}
          home={x.home}
          id={index}
          deleteCard={() =&gt; deleteCard(x)}
          key={index}
        /&gt;
      ))}
    &lt;/&gt;
  );
}

It's pretty clear that your issue is that the id property on the objects do not correspond to the objects' index in the array.

答案2

得分: 0

我怀疑你的物品没有一个 index 属性

let newFriends = friendsState.filter((x, i) => {
  return i != index;
});
英文:

I doubt that your items have an index property

let newFriends = friendsState.filter((x, i) =&gt; {
  return i != index;
});

huangapple
  • 本文由 发表于 2023年5月30日 03:05:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定