简体   繁体   English

在哪里迭代 JSON 对象

[英]Where to iterate through JSON object

I'm building something with Angular and using .我正在使用 Angular 构建一些东西并使用

My 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:可以说我想:

  1. count the amount of total children and grandchildren that john has.计算 john 的子孙总数。
  2. count the number of grandchildren that john has.数一数约翰有多少孙子孙女。

Well then:好吧:

  1. Where would I write the function for this when using Angular/Redux?使用 Angular/Redux 时,我会在哪里为此编写函数? Would I do it before I get my state?在我得到我的状态之前我会做吗? After I make my API call and get my state?在我调用 API 并获取状态之后? In the reducer?在减速机? In the component?在组件中?
  2. What is the fastest/best way to iterate and get those 2 counts that I wanted?迭代并获得我想要的 2 个计数的最快/最佳方法是什么? Because I know Angular has some built in things, would it have any faster way to do this?因为我知道 Angular 有一些内置的东西,它有没有更快的方法来做到这一点? Or through plain JS?还是通过普通的JS?

And last point:最后一点:

  1. Lets say I have some sibling component (john's wife, sue) that will be using this similar count or information.假设我有一些兄弟组件(约翰的妻子,苏)将使用这个类似的计数或信息。 If I want to keep my code DRY, where should I be making these count functions?如果我想让我的代码保持干燥,我应该在哪里制作这些计数函数?

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.

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