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
const resultIndex = Math.floor(index / 2)
index here is ininitial array index, resultIndex - new index of current element (chunk index actually).
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('+')))