Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to replace array commas with different characters in JS?

Given this array ar and a list of separators:

let ar = ["bestes", "haus", "Tiangua"];
const separators = [" ", ""];

How can I convert it into a string, while using as separator each value from the separators array, instead of the usual commas?

Expected result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

res = ["bestes hausTiangua"]

This is my current implementation, but it would use the same separator only.

let kw = ar.join(',').replace.(/,/g, '/')

>Solution :

Iterate over the array and shift from the separators every index.

const ar = ["bestes", "haus", "Tiangua"];
const separators = [" ", ""];

let str = ar[0];
for (let i = 1; i < ar.length; i++) {
  str += separators.shift() + ar[i];
}
console.log(str);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading