[英]JS reference array object what am I doing wrong?arra
我正在嘗試通過 JS 引用 React 中的 props 數組。 這是根據 console.log 稱為“selectedProps”的數組輸出:
[0: {selectedSeverity: "warning", selectedDomain: "JUNIPER"}]
我試圖通過調用 selectedProps[0].selectedSeverity 來訪問 selectedSeverity,但出現錯誤。 我知道這應該很容易,但我無法弄清楚我做錯了什么。
您的數組格式錯誤。 嘗試這個:
var selectedProps = [{0: {selectedSeverity: "warning", selectedDomain: "JUNIPER"}}]
然后訪問一個屬性,如:
selectedProps[0][0].selectedSeverity
請注意,這有點奇怪,因為您將屬性鍵命名為 0,這也是您訪問數組中第一個元素的方式。 考慮將您的對象聲明更改為以下內容:
var selectedProps = [{info : {selectedSeverity: "warning", selectedDomain: "JUNIPER"}}]
然后您可以像這樣訪問它:
selectedProps[0].info.selectedSeverity
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.