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

163
Vistas
How to join an n-dimension array with different separators for each level?

Sample input

Array

Here I am showing a 3-dimension array but the actual number of dimensions vary and is known as n.

[
  [
    [1,2],
    [3,4]
  ],
  [
    [5,6],
    [7,8]
  ]
]

Separators

It has the same length (n) as the number of dimensions of the array where the i-th element represent the separator of the i-th level of the array.

[',', '_', '-']

Desired output

1-2_3-4,5-6_7-8

What I've tried

It works for a 3-dimension array but not for a 4-dimension one.

I know what is going wrong with my code but I have no idea how to fix it.

Besides, I think there are simpler and/or more efficient methods.

3-dimension (working)

const array = [[[1,2],[3,4]],[[5,6],[7,8]]];
const separators = [',', '_', '-'];
const _separators = separators.reverse();
let i;
function join(array, first = false) {
  const next = Array.isArray(array[0]);
  let result;
  if (next) {
     result = array.map(e => {
       if (first) { i = 0; }
       return join(e);
     });
     i++;
     result = result.join(_separators[i]);
  }
  else {
    result = array.join(_separators[i]);
  }
  return result;
}
const result = join(array, true);
console.log(result);

4-dimension (not working properly)

const array = [[[[1,2],[3,4]],[[5,6],[7,8]]],[[['A','B'],['C','D']],[['E','F'],['G','H']]]];
const separators = ['|', ',', '_', '-'];
const _separators = separators.reverse();
let i;
function join(array, first = false) {
  const next = Array.isArray(array[0]);
  let result;
  if (next) {
     result = array.map(e => {
       if (first) { i = 0; }
       return join(e);
     });
     i++;
     result = result.join(_separators[i]);
  }
  else {
    result = array.join(_separators[i]);
  }
  return result;
}
const result = join(array, true);
console.log(result);
// desired output: 1-2_3-4,5-6_7-8|A-B_C-D,E-F_G-H

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

0

Something like this with recursion

const join = (array, separators, depth) => {
  if (depth < separators.length -1) {
    return array.map(el => join(el, separators, depth + 1)).join(separators[depth]);
  } else {
    return array.join(separators[depth]);
  }
};

{
  const array = [[[1,2],[3,4]],[[5,6],[7,8]]];
  const separators = [',', '_', '-'];
  console.log(join(array, separators, 0));
}
{
  const array = [[[[1,2],[3,4]],[[5,6],[7,8]]],[[['A','B'],['C','D']],[['E','F'],['G','H']]]];
  const separators = ['|', ',', '_', '-'];
  console.log(join(array, separators, 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