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

161
Vistas
Better way to modify values in a single array to different format

This may be a duplicate question. But I didn't find any similar questions in this forum.

I'm trying to modify values in an array to different format.

arrayInput = ['A', 'B', 'C', 'D', 'E', 'F'];

arrayOutput= ['A;B', 'C;D', 'E;F'];

I got the solution with the following approach

let arrayInput = ['A', 'B', 'C', 'D', 'E', 'F']
arrayOutput = [arrayInput[0]+';'+ arrayInput[1], arrayInput[2]+';'+ arrayInput[3]];
if (arrayInput[4]) {
     let val = arrayInput[4] + (arrayInput[5] ? ';'+arrayInput[5] : '');
     arrayOutput.push(val);
}
console.log(arrayOutput);

But I am looking for a generic solution such that even if I have more items in array, it should generate the output in desired format.

['A', 'B', 'C', 'D', 'E', 'F', 'G'] ==> ['A;B', 'C;D', 'E;F', 'G']
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'] ==> ['A;B', 'C;D', 'E;F', 'G;H']

Thanks for the support

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

0

Use a for loop, and increment it by 2 every iteration:

const arrayInput = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];

const res = [];
for (let i = 0; i < arrayInput.length; i += 2) {
  const part1 = arrayInput[i];
  const part2 = arrayInput[i + 1];
  if (part1) {
    res.push(part1 + (part2 ? (';' + part2) : ''));
  }
}
console.log(res);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Array#reduce as in the following demo. Just in case there's an odd number of elements, I have included a test here ${arr[i+1] ? ";" + arr[i+1] : ""}; otherwise use ${arr[i+1]}, if you always have an even number of elements.

const arrayInput = ['A', 'B', 'C', 'D', 'E', 'F'],
      arrayOutput = arrayInput.reduce(
          (acc,cur,i,arr) => 
          i % 2 === 0 ? 
              [...acc,`${cur}${arr[i+1] ? ";" + arr[i+1] : ""}`] : 
              acc,
          []
      );

console.log( arrayOutput );
//OUTPUT: ['A;B', 'C;D', 'E;F'];

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can try something like this (implementation based on array reduce and join):

const arrayInput = ['A', 'B', 'C', 'D', 'E', 'F'];

let temp = [];

const output = arrayInput.reduce((accumulator, item) => {
  if (temp.length < 2) {
    temp.push(item);
  } else {
    accumulator.push(temp.join(';'));
    temp = [];
  }
  return accumulator;
}, []);

console.log(output)

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