Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
How to create a request via iteration in Javascript

I am building a request that has multiple methods attached to it. I'd like to simplify the code by iterating over an enum and building the request, but when I try this I end up with a different result than expected.

The before code looks like this:

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'm attempting to iterate instead like so:

  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

Where my ENUM_OBJECT looks like this:

 export const ENUM_OBJECT: Partial<
  Record<StatusType, MyFilterTypes>
> = {
  [StatusType.Adopted]: setAnimalFilter1(),
  [StatusType.Lost]: setAnimalFilter2(),
  [StatusType.Appointment]: setAnimalFilter3(),
  [StatusType.Processing]: setAnimalFilter4(),
};

The first returns an actual request, the second returns a Map. I'm not sure with the ForEach how to return a request after iteration. Any clues what I'm doing wrong here?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Well, turns out the solution was dumb:

In my attempted iteration I returned return requestCounts. Changing it to return request worked.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!