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

152
Views
Combining subsequent elements of an array in Javascript

I have an array as following:

arr1 = CP-en,AR-es,CP-en,BE-fr,CP-en,CZ-cs

How do I combine the array's subsequent elements so I get the following result:

result = CP-en+AR-es, CP-en+BE-fr, CP-en+CZ-cs

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

  1. You need split your array into chunks (2 element per chank), for example by indexing like this:
const resultIndex = Math.floor(index / 2)

index here is ininitial array index, resultIndex - new index of current element (chunk index actually).

  1. Join them as you wish using .join():
result.map(chunk => chunk.join('+'))

Full code:

const arr = [ 'CP-en', 'AR-es', 'CP-en', 'BE-fr', 'CP-en', 'CZ-cs' ]
const result = []

for (let index in arr) {
  const resultIndex = Math.floor(index / 2)
  if (!result[resultIndex]) result[resultIndex] = []
  result[resultIndex].push(arr[index])
}

console.log(result.map(chunk => chunk.join('+')))

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!