[英]Where to iterate through JSON object
I'm building something with Angular and using redux .我正在使用 Angular 构建一些东西并使用redux 。
My json object has objects that have certain values.我的json对象具有具有特定值的对象。 Lets pretend:让我们假装:
name: "john",
sex: "m"
children: [
{
name: "joe",
sex: "m"
children: [
{
name: "mary",
sex: "f"
children: [
{
name: "ryan",
sex: "m"
children: []
}
]
}
]
],
name: "sue",
sex: "f"
children: [
{
name: "joe",
sex: "m"
children: [
{
name: "mary",
sex: "f"
children: [
{
name: "ryan",
sex: "m"
children: []
}
]
}
]
]
Lets say I want to:可以说我想:
Well then:好吧:
And last point:最后一点:
Do I use my state from before if some information count is going to be different?如果某些信息计数会有所不同,我是否使用之前的状态? Lets say this is only counting female children/granddaughters.可以说这只是计算女童/孙女。 And john's is for male children/grandsons. john's 是给男孩/孙子的。
Or should I be making two slice of state for these (m & f) and working off those?或者我应该为这些(m&f)制作两个状态片并处理它们?
File Structure:
app
|store
|actions
|models
|reducers
|effects
|views
|john
|.html
|.ts
|sue
|.html
|.ts
|app.ts etc
I think a lot of your points can be answered by using selectors
.我认为您的很多观点都可以通过使用selectors
来回答。
https://ngrx.io/guide/store/selectors https://ngrx.io/guide/store/selectors
1.) You can make many selectors. 1.) 您可以制作许多选择器。 Maybe you can make a selector that takes the input of the name (string) and can give you the number of children it has.也许您可以制作一个选择器,它接受名称(字符串)的输入,并可以为您提供它所拥有的孩子的数量。 And another selector that gives you the number of grandchildren this name has.另一个选择器可以为您提供此名称的孙子数量。 You can then combine it in your component.然后,您可以将其组合到您的组件中。 Maybe you can make one selector that counts both of them.也许您可以制作一个同时计算两者的选择器。 It's up to you.由你决定。
2.) Answered in 1. 2.) 在 1 中回答。
3.) You would write it as a selector in maybe selectors.ts
inside of the store folder and can then pass these selector functions in the select
argument of the store. 3.) 您可以将它作为选择器写入 store 文件夹内的也许selectors.ts
中,然后可以在 store 的select
参数中传递这些选择器函数。
4.) The fastest and best way will be through using plain JS or a JS library like lodash. 4.) 最快最好的方法是使用普通的 JS 或像 lodash 这样的 JS 库。 Angular has a lot of stuff built in but it is a UI/View library, not a make common JavaScript operations faster library. Angular 内置了很多东西,但它是一个 UI/View 库,而不是一个让普通 JavaScript 操作更快的库。
5.) I would have one slice of state and like mentioned previously using selector functions to transform the state into the data I am interested in. 5.) 我会有一个状态片,就像前面提到的那样,使用选择器函数将状态转换为我感兴趣的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.