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

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

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

问题

  1. 能有人请解释一下为什么这个筛选器不起作用吗?在“朋友卡片”上点击删除按钮应该重新打印出列表,减去了已删除的卡片。然而,虽然警报出现了,但卡片列表并没有改变。问题出在哪里?
  2. 编辑:根据评论调整了代码,但仍然没有改变任何东西
  3. 编辑2:等等,不对,第一次就对了。意识到 x.index 没有意义,因为我没有索引属性
  4. export default function App() {
  5. const [friendsState, setFriendsState] = useState(friends);
  6. function deleteCard(index) {
  7. alert("删除");
  8. let newFriends = friendsState.filter((x) => {
  9. return x.id != index;
  10. });
  11. setFriendsState(newFriends);
  12. }
  13. return (
  14. <>
  15. {friendsState.map((x, index) => (
  16. <Card
  17. name={x.name}
  18. home={x.home}
  19. id={index}
  20. deleteCard={() => deleteCard(index)}
  21. key={index}
  22. />
  23. ))}
  24. </>
  25. );
  26. }
英文:

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

  1. export default function App() {
  2. const [friendsState, setFriendsState] = useState(friends);
  3. function deleteCard(index) {
  4. alert(&quot;Deleting&quot;);
  5. let newFriends = friendsState.filter((x) =&gt; {
  6. return x.id != index;
  7. });
  8. setFriendsState(newFriends);
  9. }
  10. return (
  11. &lt;&gt;
  12. {friendsState.map((x, index) =&gt; (
  13. &lt;Card
  14. name={x.name}
  15. home={x.home}
  16. id={index}
  17. deleteCard={() =&gt; deleteCard(index)}
  18. key={index}
  19. /&gt;
  20. ))}
  21. &lt;/&gt;
  22. );
  23. }

答案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:

  1. export default function App() {
  2. const [friendsState, setFriendsState] = useState(friends);
  3. function deleteCard(cardToDelete) {
  4. alert("Deleting");
  5. let newFriends = friendsState.filter((x) => {
  6. return x !== cardToDelete;
  7. });
  8. setFriendsState(newFriends);
  9. }
  10. return (
  11. <>
  12. {friendsState.map((x, index) => (
  13. <Card
  14. name={x.name}
  15. home={x.home}
  16. id={index}
  17. deleteCard={() => deleteCard(x)}
  18. key={index}
  19. />
  20. ))}
  21. </>
  22. );
  23. }

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:

  1. export default function App() {
  2. const [friendsState, setFriendsState] = useState(friends);
  3. function deleteCard(cardToDelete) {
  4. alert(&quot;Deleting&quot;);
  5. let newFriends = friendsState.filter((x) =&gt; {
  6. return x !== cardToDelete;
  7. });
  8. setFriendsState(newFriends);
  9. }
  10. return (
  11. &lt;&gt;
  12. {friendsState.map((x, index) =&gt; (
  13. &lt;Card
  14. name={x.name}
  15. home={x.home}
  16. id={index}
  17. deleteCard={() =&gt; deleteCard(x)}
  18. key={index}
  19. /&gt;
  20. ))}
  21. &lt;/&gt;
  22. );
  23. }

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 属性

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

I doubt that your items have an index property

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

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:

确定