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

204
Views
How to map an response when It's array and when It's not?

I just coded two simple functions to validate a field if it has a specific value, but in some cases the responses could be a array. Can I simplify these function in just one function? I tried to use the same "isOrderInLastMile" function when the response is just a object but I retrieve the "object.map is not a function" error.

function 1: I use this function when the response is an array of objects.

export function isOrderInLastMile(pieces: any): boolean {
  const totalPieces: number = pieces.length;
  let momentFound: number = 0;

  pieces.map((element) => {
    if (
      element.stagesHistory &&
      getCurrentMoment(element.stagesHistory) ===
        MomentsLabel.En_última_milla_id
    ) {
      momentFound = momentFound + 1;
    }
    return momentFound;
  });

  return totalPieces === momentFound;
}

Function 2: I use this function when this response is just an object

export function isPieceInLastMile(piece: any): boolean {
  return (
    piece.stagesHistory &&
    getCurrentMoment(piece.stagesHistory) === MomentsLabel.En_última_milla_id
  );
}

How Can I unify both functions? Thanks!!! :)

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

0

Either wrap your single element in an array, like this:

export function isPieceInLastMile(piece: any): boolean {
  return isOrderInLastMile([piece]);
}

or use the single function in a callback

  return pieces.every(isPieceInLastMile);

(The latter option can be used instead of counting all matching items)

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!