I'm currently working on a request that involves multiple methods, and I want to streamline the code by using an enum to iterate through and construct the request. However, my attempt at doing this has resulted in unexpected outcomes.
The original code is as follows:
export const makeShelterRequest = (area: string) => {
const request = new Request().setArea(area);
request
.getDogs()
.set(
StatusType.Adopted,
setAnimalFilter1(),
)
.set(
StatusType.Lost,
setAnimalFilter2(),
)
.set(
StatusType.Appointment,
setAnimalFilter3(),
)
.set(
StatusType.Processing,
setAnimalFilter4(),
));
return request;
};
I've tried iterating like this instead:
const request = new Request().setArea(area);
const requestCounts = request.getDogsCount()
const ENUM_OBJECT_KEYS = Object.keys(ENUM_OBJECT)
ENUM_OBJECT_KEYS.forEach((key) => {
requestCounts.set(
key,
ENUM_OBJECT[key as StatusType]))
})
return requestCounts
This is what my ENUM_OBJECT looks like:
export const ENUM_OBJECT: Partial<
Record<StatusType, MyFilterTypes>
> = {
[StatusType.Adopted]: setAnimalFilter1(),
[StatusType.Lost]: setAnimalFilter2(),
[StatusType.Appointment]: setAnimalFilter3(),
[StatusType.Processing]: setAnimalFilter4(),
};
The original code returns an actual request while the iteration version returns a Map. I'm unsure how to modify the ForEach loop to return a request after the iteration. Any insights into what might be incorrect in my approach?