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

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
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
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
harwind
Consider the following bits of code: void foo(const int i) // First foo { std::cout &lt;&lt; "First " &lt;&lt; i &lt;&lt; endl; } vo...
New
harwind
I have an array of objects in JavaScript, and I want to sort them based on a specific property of the objects. For example, I have an arr...
/js
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’m working on a SQL query for a database containing records of customer transactions. Each transaction has a transaction_id, customer_id...
New
harwind
I’m considering learning new languages to expand my programming skills in 2023. While aware of popular choices, I seek advice on striking...
New
Tazmeen
Hello, I am new to this forum. Not really sure if this topic is relevant for this chat at all. I apologize if its not. I am trying to c...
New

Other popular topics Top

axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
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
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
Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
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
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New
PragmaticBookshelf
A Ruby-Centric Chat with Noel Rappin @noelrappin Once you start noodling around with Ruby you quickly figure out, as Noel Rappi...
New