[英]How to return the correct values at with this array format in javascript?
我有這樣的數組:
const tab = [
{ '1': {'id':'me'}},
{ '2': {'id':'you'}}
];
我創建了這個函數來從最后一個數組中獲取值,如下所示:
const ar = tab.map((user,i)=>{
return(
<div>
<p>{tab[i][1].id.toString()}</p>
</div>
)
});
筆記:
我想在不更改'1'
或'2'
情況下獲得正確的 id 值,因為如果我將'1'
或'2'
更改為'A'
,例如我可以簡單地編寫tab[i].A.id.toString()
像這樣的事情可能對你有用:
const tab = [
{ '1': { id: "me" }},
{ '2': { id: "you" }}
];
const ar = tab.map((user, i) => {
return (
<div>
<p>{user[(i + 1).toString()].id}</p>
</div>
);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.