Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
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 answers
Answer question

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 Report

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 Report

0

const list = ['abc', 'yte', 'juu'];
const result = list.map(value => `(${value})`).join("");
console.log(result);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!