简体   繁体   中英

JS reference array object what am I doing wrong?arra

I am trying to reference a props array in React via JS. Here is the array output called "selectedProps" as per console.log:

[0: {selectedSeverity: "warning", selectedDomain: "JUNIPER"}]

I am trying to access selectedSeverity by calling selectedProps[0].selectedSeverity but am getting an error. I know this should be easy but I can't figure out what I am doing wrong.

Your array is malformed. Try this:

var selectedProps = [{0: {selectedSeverity: "warning", selectedDomain: "JUNIPER"}}]

Then access a property like:

selectedProps[0][0].selectedSeverity

Note that this is a little odd because you are naming a property key 0 and that is also how you access the first element in the array. Consider changing your object declartion to something like:

 var selectedProps = [{info : {selectedSeverity: "warning", selectedDomain: "JUNIPER"}}]

Then you access it like:

selectedProps[0].info.selectedSeverity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM