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

148
Vistas
Javascript join array with brackets

In javascript I need to join an array into a string with brackets. For example ['abc', 'yte', 'juu'] => (abc)(yte)(juu). I need the fastest and cleanest way to do this. I tried using the array join operator but it doesnt work on the first and last element.

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

0

You can do it with template literals. Use join() with )( separator, and wrap the result with ():

const data = ['abc', 'yte', 'juu'];

const result = `(${data.join(')(')})`;
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Thinking "I need the fastest and cleanest way to do this." is often not ideal. In most cases what people want is the most readable and understandable way to implement a solution.

I quite like the following. Where we first bracket each item in the array with map and then join the items with no delimiter:

const data = ['abc', 'yte', 'juu'];
let brackets = data.map(x => "(" + x + ")");
let result = brackets.join("")
console.log(result);

That of course takes two passes over the array. If you really must have the fastest solution just use a for loop:

const data = ['abc', 'yte', 'juu'];
let result = "";
for (let item of data) {
    result += "(" + item + ")";
}
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

const list = ['abc', 'yte', 'juu'];
const result = list.map(value => `(${value})`).join("");
console.log(result);
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