如何获取没有括号且没有换行符的数组元素?

How can I get the elements of an array without the brackets and no line break?

我想要的:

var word = ["happy", "penguin", "sword"]

message.channel.send(word)

//prints 
"happy", "penguin", "sword"

实际发生了什么:

var word = ["happy", "penguin", "sword"]

message.channel.send(word)
//prints
happy
penguin
sword

这是在 discord.js,谢谢。

将数组连接成一个字符串,然后将其发送。

const word = ["happy", "penguin", "sword"];

const str = `"${word.join('", "')}"`;

console.log(str);

其他文档

我建议您使用 JSON.stringify 映射所有数组值:

const word = ["happy", "penguin", "sword", "the \"craziest\" thing ever"];

const str = word.map(w => JSON.stringify(w)).join(", ");

console.log(str);

如果您不知道 Array.prototype.map,这是一个非常有用的函数,它循环遍历每个值并 returns 一个包含新值的数组。

var word = ["happy", "penguin", "sword"]

word.forEach(el => console.log(el))
/*
happy
penguin
sword
*/

console.log(word.toString())
// happy,penguin,sword

console.log(`"${word.join('", "')}"`)
// "happy", "penguin", "sword"