簡體   English   中英

如何使用對象列表在 React 中進行分解

[英]How I can make a desctruction in React with a list of objects

嘿我正在嘗試根據每個用戶的 id 刪除一個用戶。 問題是我不知道如何根據 id 查找用戶。 當我像這樣破壞

 console.log(patient[0] && patient[0].id);

我從控制台女巫的用戶那里得到了 id 很好。 但是當我寫:

 console.log(patient && patient.id);

我得到了一個不設防的回應。 這是代碼:

 const [patient, setPatient] = useState([]); let authtoken = localStorage.getItem("token"); async function getPatients() { const serverResponse = await Axios.get("http://localhost:8080/patients", { headers: { Authorization: authtoken, }, }); setPatient(serverResponse.data); console.log(serverResponse.data); } useEffect(() => { getPatients(); }, []); console.log(patient[0] && patient[0].id);

和我的控制台圖像。 在此處輸入圖片說明

在這里,我應該銷毀我的對象列表,並根據我想刪除用戶的 id,但我的 id 是 undefiend。 但我不想指定一個 Patient[0].id 我想從我刪除的用戶那里獲取 id。

如果對象是 const Obj = {Car:2,Bike:3,Plane:1}; 然后

const { 汽車、自行車、飛機 } = Obj;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM