I'm building something with Angular and using redux .
My json object has objects that have certain values. 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.
Or should I be making two slice of state for these (m & f) and working off those?
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
.
https://ngrx.io/guide/store/selectors
1.) You can make many selectors. 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.
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.
4.) The fastest and best way will be through using plain JS or a JS library like lodash. Angular has a lot of stuff built in but it is a UI/View library, not a make common JavaScript operations faster library.
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.
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.