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
}
}
]
}
}