Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

0

const list = ['abc', 'yte', 'juu'];
const result = list.map(value => `(${value})`).join("");
console.log(result);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda