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

197
Vistas
Javascript: la forma más corta de escribir múltiples condiciones if dentro del ciclo forEach sin escribir un método separado

Tengo debajo de cada bucle

 this.cfData.customAttributes.forEach(function (currentObj) { if(currentObj.inputValues !== null) { currentObj.inputValues = Array.prototype.map.call(currentObj.inputValues, function(item) { return item.value; }).join(", "); } if(currentObj.objectType !== null){ currentObj.objectType = currentObj.objectType.toLowerCase().replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase()); } if(currentObj.dataType !== null){ currentObj.dataType = currentObj.dataType.toLowerCase().replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase()); } if(currentObj.isGridEligible !== null){ currentObj.isGridEligible = currentObj.isGridEligible.toLowerCase().replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase()); } if(currentObj.isInvoiceEligible !== null){ currentObj.isInvoiceEligible = currentObj.isInvoiceEligible.toLowerCase().replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase()); } });

Me preguntaba si hay alguna forma más corta de escribir estas condiciones dentro del bucle. Por favor recomiende.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Estaré respondiendo a un código más genérico, si me lo permiten:

  1. En primer lugar, si está utilizando declaraciones individuales, los corchetes ( {} ) alrededor de if s se pueden eliminar.
 objArr.forEach(obj => { if(obj.propA !== null) obj.propA = getPropA(obj); if(obj.propB !== null) obj.propB = getPropB(obj); ... });
  1. Siempre puede crear un código demasiado complicado con funciones asignadas ( getPropA , getPropB , etc.).
 const getProps = { propA(obj){ return ...; }, propB: obj => ..., ... }; objArr.forEach(obj => { // Object.entries receives an object, and returns an array // in the form [ key, value ][] for(var [ prop, getter ] of Object.entries(getProps)) if(obj[prop] !== null) obj[prop] = getter(obj); });
about 4 years ago · Juan Pablo Isaza Denunciar

0

Puedo recomendar este código que es mucho más simple y corto.

 this.cfData.customAttributes.forEach(function (currentObj) { const arr = ["objectType", "dataType", "isGridEligible", "isInvoiceEligible"]; if (currentObj.inputValues !== null) { currentObj.inputValues = Array.prototype.map.call(currentObj.inputValues, function (item) { return item.value; }).join(", "); } arr.forEach(item => { if (currentObj[item] !== null) { currentObj[item] = currentObj[item].toLowerCase().replace(/(^\w{1})|(\s{1}\w{1})/g, match => match.toUpperCase()); } }); });
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