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

358
Vistas
Can I combine array destructuring and .trim()?

I am having an array of strings coming from a CSV file which I destructure in my node.js app.

Now I need the strings trimmed with .trim() but I wonder if there is an immediate way to do this. The below doesn't work:

// writing object with returned arrays retrieved from CSV
  playerRecordsArr.forEach((playerRecord) => {
    const [id.trim(), customFlag.trim(), countryCode.trim()] = playerRecord;
    resultObject[steamID] = {
      playerData: { customFlag, countryCode },
    };
  });

I guess the way to do it would be this, but I'd lose the destructuring goodness:

// writing object with returned arrays retrieved from CSV
  playerRecordsArr.forEach((playerRecord) => {
    const id = playerRecord[0].trim();
    const customFlag = playerRecord[1].trim();
    const countryCode = playerRecord[2].trim();
    resultObject[steamID] = {
      playerData: { customFlag, countryCode },
    };
  });
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

map can be used to transform all elements of an array, but I would recommend to do apply trim individually at the place where you are using the value:

for (const [id, flag, country] of playerRecordsArr) {
    resultObject[id.trim()] = {
        playerData: { customFlag: flag.trim(), countryCode: country.trim() },
    };
}
over 4 years ago · Santiago Trujillo Denunciar

0

const playerRecord = ['   one      ', 'two ', 10000];

const trimIfString = x => typeof x === 'string' ? x.trim() : x;

const [id, customFlag, countryCode] = playerRecord.map(trimIfString);

console.log(playerRecord)
console.log(playerRecord.map(trimIfString))

over 4 years ago · Santiago Trujillo 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