从JS数组创建字符串

nem*_*man -2 javascript arrays string

我有阵列:

const arr = ["This is Sparta", "Yes it is", "Hello"];

我想要像这样的字符串输出:

const str = "This is Sparta, Yes it is, Hello"

所以,在数组中的每个逗号之后我需要在字符串中使用break line.

Art*_*yan 6

.join()将您的数组值连接到一个字符串中,并在每个值后面添加您作为参数传递给它的字符,因此,\n将在每个单词后面添加逗号并\n添加换行符

const arr = ["This is Sparta", "Yes it is", "Hello"];

console.log(arr.join(",\n"));
Run Code Online (Sandbox Code Playgroud)