Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
Optimising conditional check in typescript

I have the following JS code were i am displaying different status based on the response key from API. is there any better approach to optimise this code so that i don’t have to check each case with IF, in case if the number of status increases

if (data.shippingStatus) {
  let shippingStatus = data.shippingStatus.toString();
  if (shippingStatus === "AWAITING_SHIPMENT") {
    shippingStatus = "Awaiting Shipment";
  } else if (shippingStatus === "SHIPPED") {
    shippingStatus = "Shipped";
  } else if (shippingStatus === "DELIVERED") {
    shippingStatus = "Delivered";
  } else if (shippingStatus === "CANCELLED") {
    shippingStatus = "Cancelled";
  }
  resData.push(setData(data.shippingStatus ? shippingStatus : ""));
}
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Try object mapper:

const statusMapper: {[key:string]: string} = {
  AWAITING_SHIPMENT: "Awaiting Shipment",
  SHIPPED: "Shipped",
  DELIVERED: "Delivered",
  CANCELLED: "Cancelled"
};

if (data.shippingStatus) {
  let shippingStatus = data.shippingStatus.toString();
  resData.push(setData(data.shippingStatus ? statusMapper[shippingStatus] : ""));
}

EDIT: Added type to the mapper

about 4 years ago · Juan Pablo Isaza Denunciar

0

There are different approaches to your question. If you're just looking for a solid solution for the current problem aka mapping values, you can either create an object mapper as the other answers suggest or just a simple function that formats your string e.g.:

var text = "AWAITING_SHIPMENT";
text = text.toLowerCase()
    .split('_')
    .map((s) => s.charAt(0).toUpperCase() + s.substring(1))
    .join(' ');
console.log(text);

But if you're looking into the subject in a broader sense, you can use dynamic dispatch via polymorphism. This is an example that uses polymorphism to change behavior based on a type.

about 4 years ago · Juan Pablo Isaza Denunciar

0

How my implementation works, it splits the status by _ and capitalize it, and finally return a new status, as required

// Dummy Data

const data = {
  shippingStatus:"AWAITING_SHIPMENT"
}


// New Proposal

if(data.shippingStatus){
  const { shippingStatus } = data;
  
  const status = 
  shippingStatus.split('_') 
  .map(string => string.charAt(0).toUpperCase() + string.slice(1).toLowerCase())
  .join(" ");
  
  console.log(status)
  
  // no need to check for data.shippingStatus twice
  // while you're still within if condition, just do the following :
  // resData.push(setDate(status))
  
}

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda