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

325
Vistas
How to merge two arrays like this way? (Example in description)

I would like to know how can I merge arrays in this way e.g.:

const names = ['MARCUS', 'LUCAS', 'ANDREA']
const surnames = ['SMITH', 'JOHNSON', 'WILLIAMS']
[...merge stuff]
// and then the output should be 
const full_names = ['MARCUS SMITH', 'LUCAS JOHNSON', 'ANDREA WILLIAMS']
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Here a zip function creates an array like [["MARCUS","SMITH"],["LUCAS","JOHNSON"],["ANDREA","WILLIAMS"]] and then map converts the inner arrays to strings.

const names = ['MARCUS', 'LUCAS', 'ANDREA'];
const surnames = ['SMITH', 'JOHNSON', 'WILLIAMS'];

const zip = (...arrays) => {
  let res = [];
  
  for(let i = 0; i < arrays[0].length; i++) {
    res.push([]);
    for(let j = 0; j < arrays.length; j++) {
      res[i].push(arrays[j][i]);
    }
  }
  
  return res;
};

const res = zip(names, surnames)
  .map(([name, surname]) => name + ' ' + surname);

console.log(res);

about 4 years ago · Juan Pablo Isaza Denunciar

0

These are couple of ways to do it. Assumption is that there is 1:1 matching of name and surname in the two input arrays.

const names = ['MARCUS', 'LUCAS', 'ANDREA']
const surnames = ['SMITH', 'JOHNSON', 'WILLIAMS']

// way 1: traditional loop
const res = [];
for (let i = 0; i < names.length; i++) {
  res.push(`${names[i]} ${surnames[i]}`);
};
console.log('full_names: ', res);

// way 2: another way - more functional flavor
const res2 = names.reduce((acc, e, i) => {
    acc.push(`${e} ${surnames[i]}`);
    return acc;
}, [])
console.log('full_names: ', res2);

output:

[ 'MARCUS SMITH', 'LUCAS JOHNSON', 'ANDREA WILLIAMS' ]
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use recursion as follows:

const names = ['MARCUS', 'LUCAS', 'ANDREA'],
      surnames = ['SMITH', 'JOHNSON', 'WILLIAMS'],
      
      fn = (n,sn,i,f) => 
          i <= n.length - 1 ? 
              fn(n,sn,i+1,[...f,`${n[i]} ${sn[i]}`]) : 
                  f;
      
      console.log( fn(names,surnames,0,[]) );

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