0

Seen below, CountTargetsData is an interface I use to parse JSON data. I want to assign the keys id and values targets to certain variables.

I have tried using a forEach() loop on the Object.values() however nothing gets printed and it breaks my code without any compile-time errors. Is there any alternative way to use forEach() or could another method do the job?

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
          Object.values(responseDeploymentData.data).forEach(state => {
            Object.values(state).forEach(date => {
              Object.values(date).forEach(index =>{  
                Object.values(index).forEach(id => {
                  Object.values(id).forEach(targets => {
                    console.log(responseDeploymentData.data.state.date.index.id.targets);
                  })
                })
              })
            })
          })
  }
);


Update Example Data

{
    "stateA": {
        "2022-06-03": [
            {
                "1855": {
                    "count_targets": 1,
                    "count_targets_excluded": 0,
                    "count_targets_pending": 0,
                    "count_targets_in_progress": 0,
                    "count_targets_completed": 0,
                    "count_targets_failed": 1
                }
            }
        ],
        "2022-06-02": [
            {
                "1849": {
                    "count_targets": 1,
                    "count_targets_excluded": 0,
                    "count_targets_pending": 0,
                    "count_targets_in_progress": 0,
                    "count_targets_completed": 1,
                    "count_targets_failed": 0
                }
            }
        ]
    },
    "stateB": {
        "2015-06-01": [
            {
                "1832": {
                    "count_targets": 9,
                    "count_targets_excluded": 0,
                    "count_targets_pending": 3,
                    "count_targets_in_progress": 0,
                    "count_targets_completed": 2,
                    "count_targets_failed": 5
                }
            }
        ],
        "2016-06-04": [
            {
                "1856": {
                    "count_targets": 1,
                    "count_targets_excluded": 0,
                    "count_targets_pending": 1,
                    "count_targets_in_progress": 0,
                    "count_targets_completed": 0,
                    "count_targets_failed": 0
                }
            }
        ]
    }
    
}
user19251203
  • 322
  • 4
  • 13
  • 1
    Try changing your `console.log(responseDeploymentData.data.state.date.index.id.targets)` to `console.log(targets)`. – Jordan Jul 07 '22 at 16:40
  • Could you please provide an example json, so everyone is able to use your code snippet? – Thomas Renger Jul 07 '22 at 19:02
  • Your `console.log` would only work if the keys are correctly, otherwise you would get an execption. – Thomas Renger Jul 07 '22 at 19:04
  • @ThomasRenger i just provided example JSON data in post. see update – user19251203 Jul 07 '22 at 19:21
  • What's `useCallback`? Do you have a [mre] you can paste into a standalone IDE we can use to demonstrate your issue for ourselves? That way we can immediately get to work solving the problem without first needing to re-create it. – jcalz Jul 07 '22 at 19:43
  • I changed your typing für my environment (otherwise is not buildable) and the console.log (my output is targets) and the output is the following => only the first one (count_targets: 1 count_targets_completed: 0, count_targets_excluded: 0, count_targets_failed: 1, count_targets_in_progress: 0, count_targets_pending: 0) ... so I think I do not have a problem. – Thomas Renger Jul 07 '22 at 20:33
  • The change in typing was because of the array your example data. `[index: string]: ...` is not correct. So I removed the `index` and used the array. But as the resulting js do not know types, it should only about compile issues and not runtime issues. But give it a try. – Thomas Renger Jul 07 '22 at 20:36

0 Answers0