简体   繁体   English

Redux 管理对象的 arrays 并查找嵌套对象

[英]Redux managing arrays of objects and finding nested objects

I'm not sure what to name this, but basically I'm new to React and Redux and looking for a more correct/cleaner way to do this or just how to do this with my current set up.我不知道该怎么命名,但基本上我是 React 和 Redux 的新手,正在寻找一种更正确/更清洁的方法来执行此操作,或者只是如何使用我当前的设置来执行此操作。 I have a state that looks like this我有一个看起来像这样的 state

--Character
---id
---name
---race
----id
----raceName
----traits
-----trait
------id
------name
------description
-----trait
------id
------name
------description
---classes
----class
-----id
-----className
-----classLevel
-----traits
------trait
-------id
-------name
-------description
------trait
-------id
-------name
-------description
----class
-----id
-----className
-----classLevel
-----traits
------trait
-------id
-------name
-------description
------trait
-------id
-------name
-------description
---traits
----trait
-----id
-----name
-----description
----trait
-----id
-----name
-----description

As you can see(hopefully) traits is an array of object TRAIT and classes is an array of object CLASS, in the end the whole state is quite a messy deal. As you can see(hopefully) traits is an array of object TRAIT and classes is an array of object CLASS, in the end the whole state is quite a messy deal. I've read that I can somehow reference them by ID's but I'm not sure how if IDs are autogenerated.我读过我可以通过 ID 以某种方式引用它们,但我不确定 ID 是否是自动生成的。

So I kind of have two questions:所以我有两个问题:

  1. How do I simplify/flatten this structure if it even could be done?如果可以做到的话,我该如何简化/扁平化这个结构?
  2. If I can't simplify this structure is there anyway I can find a specific Trait with a specific ID without looping through all the objects that have property traits?如果我不能简化这个结构,我是否可以找到具有特定 ID 的特定 Trait 而无需遍历所有具有属性特征的对象?

Yes.是的。 You can find Trait with a specific ID easily.您可以轻松找到具有特定 ID 的Trait Let know if this is what you are asking.让知道这是否是您要问的。

// Search in traits directly under Character.

const traitForId = this.state.Character.traits.find((trait) => {
   return trait.id = "<SPECIFIC_ID>"
})


// Search in the list of traits under each Class.

const classTraits = this.state.Character.classes.map((class) => class.traits).flat();
const classTraitsForId = classTraits.find((trait) => {
   return trait.id = "<SPECIFIC_ID>"
})

Find below recursive way to find a Trait irrespective of where it's present in the state.查找以下递归方式来查找特征,而不管它在Trait中的位置。

function findTraitForId(state, specific_id){
 if(state.traits){
   const traitForId = state.traits.find((trait) => {
      return trait.id == specific_id
   });
   if(traitForId)
      return traitForId;
 }
 return Object.keys(state).filter((key) => key != 'traits').map((stateItem) => {
   return findTraitForId(state[stateItem], specific_id);
 }).flat();
}

Tried above function for the input在 function 上尝试输入

findTraitForId({'classes':[{traits: [{id: 1, name: "A"}, {id: 2, name: "AB"}]}, {traits: [{id: 3, name: "ABC"}, {id: 4, name: "ABCD"}]}], traits: [{id: 5, name: "ABCDE"}, {id: 6, name: "ABCDEF"}]}, 3)

which return哪个返回

[{id: 3, name: "ABC"}]

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

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