简体   繁体   中英

Why this object array access inconsistency? I don't quite understand it as a newb

Hey stackoverflow people, as you can see here https://cdn.discordapp.com/attachments/477912057560432680/632327022856896522/unknown.png I had to inconsistently call values of objects of arrays.

And I don't really understand why that is.

FYI: projectType in the picture above is either the contents of Design Object-Array, or Development Object-Array. Depending on the current path.

Minimal example:

const data = {
 design: [{
  attachments: [{
   value: ""
  }]
 }]
}

const [state, setState] = useState(data)

//I have to call that like
state.attachments[0].value

-----------------------------------------------------------------------

const data = {
design: [{
 value: ""
}]
}

const [state, setState] = useState(data)

//I can call that like
state.value

The state is still like state = [{ value: "" }] and has an index of [i], when I log it. Just like the child Object-Array.

So why cant I call it like I can do with the parent?

Appreciate any help.

I feel quite lost if I don't even understand such basic things.

Well that was quite a dumb question I feel.

I am so tired that I forgot that I mapped my Object-Array like

projectsType.map( item => ())

Since it calls my arrow function on every element of the Object-Array then, thats why I can access it without an index value.

Sorry for that inconvenient post.

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