harwind

harwind

Converting an Array to a String in JavaScript

I have an array of strings in JavaScript, and I need to convert it into a single string with specific delimiter characters between the elements. Here’s a sample array:

let myArray = ["apple", "banana", "cherry", "date"];

I want to convert this array into a single string with a comma and space (", ") between each element. The desired output should look like this:

"apple, banana, cherry, date"

Please send me the JavaScript code I need to do this conversion. If I wish to use a different character or characters between the array components, how can I change the delimiter? I looked for a solution by visiting many sources, but I was unsuccessful. Please help me with this, Thank you.

/js

Most Liked

PeterKeyse

PeterKeyse

Here’s an example of how I string together the keywords from an JSon array:
Keywords = Data.keywords_and_keyphrases
KeywordString = 'Keywords: ’
for (let i = 0; i < Keywords.length; i++) {
Keyword = Keywords[i]
KeywordString = KeywordString + Keyword + ', ’
}
As you can see they’re separated by a comma + ‘, ’ which could equally be +’: ’

mindriot

mindriot

In your example you could just do:

myArray.join(“, “);

You can pass any string to the join method as a delimiter. See Array Join method documentation

gulshan212

gulshan212

Well, you have to use the join method to do what you are looking for.

let myArray = ["apple", "banana", "cherry", "date"];
let delimiter = ", "; // You can change this to your desired delimiter

let result = myArray.join(delimiter);

console.log(result);

Thanks

Popular General Dev topics Top

AstonJ
The obligatory speed test thread :smiley: Check here: https://www.speedtest.net When complete, click on the share link and copy and pas...
New
AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
AstonJ
Which apps do you think are killing it right now? Either from a technical perspective or ones that you like personally or feel have been...
New
foxtrottwist
Here’s our thread for the Keyboardio Atreus. It is a mechanical keyboard based on and a slight update of the original Atreus (Keyboardio ...
New
Exadra37
I am a Linux user since 2012, more or less, and I always use Ubuntu on my computers, and my last 2 laptops have been used Thinkpads, wher...
New
mafinar
I always start with excitement and then get busy on 9/10th day. This year, like the year before this, and the year before that, I intend ...
New
New
First poster: mindriot
LG 28-inch 16:18 DualUp Monitor with Ergo Stand and USB Type-C™ (28MQ780-B) | LG USA. Shop LG 28MQ780-B on the official LG.com website ...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New

Other popular topics Top

AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
PragmaticBookshelf
A Hero’s Journey with Chris Pine @chrispine Chris Pine, author of Learn to Program, Third Edition, discusses his journey to beco...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File &gt; New Rule: And select Deny, O...
New