简体   繁体   English

为什么这个 object 阵列访问不一致? 作为新手我不太理解

[英]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.嘿,stackoverflow 的人,正如你在这里看到的https://cdn.discordapp.com/attachments/477912057560432680/632327022856896522/unknown.png我不得不不一致地调用 ZA3CBC3CC4F9D0CE2D71D9555 对象的值

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.仅供参考:上图中的projectType要么是Design Object-Array 的内容,要么是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.当我记录它时,state 仍然像state = [{ value: "" }]并且索引为 [i]。 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.因为它在对象数组的每个元素上调用我的箭头 function,所以我可以在没有索引值的情况下访问它。

Sorry for that inconvenient post.很抱歉那个不方便的帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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