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

Where Next?

Popular General Dev topics Top

Jase
Do they publish their stacks or is it mostly a case of guesswork? Twitter facebook instagram snapchat tiktok google et all. Used to be we...
New
AstonJ
This article got me thinking about encrypted chat: Europol said that French police had discovered some of EncroChat’s servers were lo...
New
brennan
Trying to understand recursion in Elixir. Sometimes it is simple based on the problem, sometimes it is hard. Any suggestions on how to le...
New
sona11
How can I apply a modified date and time to a variable? This is what I get when I execute the following query in SQL Server Mgmt Studio: ...
New
finner
Hello devtalk ! Heroku are closing their free tier (dynos) later this month and I was wondering if you guys could recommend any alternat...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
PaulMartin
Hey everyone! Do you have any tips or free resources that can help me learn Rspec? Although I know how to write some Rspec, I’m not very...
New
thetoaderseventytwo
I’ve been trying to dip my feet into using Unity and C# for the sake of developing games, however, I have barely any knowledge of how to ...
New
harwind
I am working on a Python script, and you encounter an error related to the misuse of lists and tuples. Here’s a simplified version of you...
New
GTX
Hello everyone! I am not a developer, just wanna know if it’s possible for someone with no skills to learn how to reverse hack my hackers.
New

Other popular topics Top

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
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
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
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
New
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New