When working with nested objects, I am trying to access the properties using Object.keys() and forEach(). However, I am encountering an issue when attempting to access the nested keys filteringState[item][el].
Is there a specific way to write a function for this scenario?
interface InitialStateTypes {
country: {
"italy": boolean,
},
board: {
"full": boolean,
},
transport: {
"bus": boolean,
"own": boolean
},
}
interface FilteringDataTypes {
country: string[],
board: string[],
transport: string[],
}
const collectFilteringData = (filteringState: InitialStateTypes) => {
let filteringData = <FilteringDataTypes>{};
Object.keys(filteringState).forEach((item) => {
Object.keys(filteringState[item]).forEach((el) => {
if (filteringState[item][el]) {
if (!filteringData[item]) {
filteringData[item] = [el];
} else {
filteringData[item] = [...filteringData[item], el];
}
}
});
});
return filteringData;
};
export default collectFilteringData;