简体   繁体   English

Typescript:使用 forEach() 解析嵌套接口数据

[英]Typescript: parse nested interface data with forEach()

CountTargetsData is an interface I use as a type to later parse JSON data. CountTargetsData是我用作稍后解析 JSON 数据的类型的接口。 As seen below, the data has many nesting.如下所示,数据有很多嵌套。

interface CountTargetsData {
  data: {
    [state: string]: {
      [date: string]: {
        [index: string]: { [id: string]: { [targets: string]: number } },
      },
    },
  };
}

const parseCounters = useCallback(
  async (responseDeploymentData: CountTargetsData) => {
    //WANT TO PARSE ID AND TARGETS HERE
  }
);

Given all the nests, how can I use the forEach() method to access the id and targets for each CountTargetsData ?鉴于所有的嵌套,我如何使用forEach()方法访问每个CountTargetsDataidtargets

Sample Data:样本数据:

    "stateA": {
        "2016-06-03": [
            {
                "1200": {
                    "count_targets": 1,
                    "count_targets_excluded": 0,
                    "count_targets_pending": 0,
                    "count_targets_in_progress": 0,
                    "count_targets_completed": 0,
                    "count_targets_failed": 1
                }
            }
        ],
        "2016-06-07": [
            {
                "1455": {
                    "count_targets": 1,
                    "count_targets_excluded": 0,
                    "count_targets_pending": 0,
                    "count_targets_in_progress": 0,
                    "count_targets_completed": 1,
                    "count_targets_failed": 0
                }
            }
        ]
         
    }
}

Update更新

Your type doesn't match the example data you provided您的类型与您提供的示例数据不匹配

It should be:它应该是:

interface CountTargetsData {
  data: {
    [state: string]: {
      [date: string]: ({
        [index: string]: {
          [id: string]: {
            [targets: string]: number
          }
        },
      })[],
    },
  };
}

 const data = { "stateA": { "2016-06-03": [{ "1200": { "count_targets": 1, "count_targets_excluded": 0, "count_targets_pending": 0, "count_targets_in_progress": 0, "count_targets_completed": 0, "count_targets_failed": 1 } }], "2016-06-07": [{ "1455": { "count_targets": 1, "count_targets_excluded": 0, "count_targets_pending": 0, "count_targets_in_progress": 0, "count_targets_completed": 1, "count_targets_failed": 0 } }] } } Object.entries(data).forEach(([state, value]) => { Object.entries(value).forEach(([date, value]) => { value.forEach(obj => { Object.entries(obj).forEach(([id, value]) => { Object.entries(value).forEach(([target, value]) => { console.log(state, date, id, target, value) }) }) }) }) })

Old answer旧答案

You can't use .forEach directly as the nested data is stored as objects and not arrays.您不能直接使用.forEach ,因为嵌套数据存储为对象而不是数组。

Try using Object.entries / Object.values first or use for..of loop:首先尝试使用Object.entries / Object.values或使用for..of循环:

const parseCounters = useCallback(
    async (
      responseDeploymentData: CountTargetsData
    ) => {
      Object.values(responseDeploymentData.data).forEach(state => {
        Object.values(state).forEach(date => {
          // and so on
        })
      })
  }
const parseCounters = useCallback(
    async (
      responseDeploymentData: CountTargetsData
    ) => {
      for (const state of responseDeploymentData.data) {
        for (const date of state) {
          // and so on
        }
      }
  }

Think about using recursion or DFS algorithm考虑使用递归或 DFS 算法

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

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