Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么join(";,";)不适用于我的数组?_Javascript_Arrays_Node.js_Discord.js - Fatal编程技术网

Javascript 为什么join(";,";)不适用于我的数组?

Javascript 为什么join(";,";)不适用于我的数组?,javascript,arrays,node.js,discord.js,Javascript,Arrays,Node.js,Discord.js,我试图制造一个不和谐的机器人,但我不明白为什么会发生这种情况。现在回想起来,这并不是一个大问题,但了解原因是一个好主意 基本上,我有一个数组,看起来像这样: // Boosters.js exports.test = ["Card", "Card2", "Card3", "Card4", "Card5", "Card6", "Card7", "Card8", "Card9", "Card10"]; 然后,在另一个文件中,我有一个函数: // Functions

我试图制造一个不和谐的机器人,但我不明白为什么会发生这种情况。现在回想起来,这并不是一个大问题,但了解原因是一个好主意

基本上,我有一个数组,看起来像这样:

// Boosters.js
exports.test = ["Card", "Card2", "Card3", "Card4", "Card5", 
                "Card6", "Card7", "Card8", "Card9", "Card10"];
然后,在另一个文件中,我有一个函数:

// Functions.js
var pack = [];

let getCards = function getCards()
{
    var obtainedCards = [];

    for(i = 0; i < 7; i++)
    {
        var cards = Boosters.test[Math.floor(Math.random() * Boosters.test.length)];
        obtainedCards.push(cards);
    }

   // Adds the cards from the obtained cards variable to the pack
   pack.push(obtainedCards);

}
这里没有问题,它是有效的。问题是,在Discord中,它看起来像:

获得的卡:卡1、卡5、卡2、卡6、卡2、卡1、卡7


基本上,它忽略了join()函数。奇怪的是,如果我打印原始测试[],bot实际上会使用join(“,”)将所有内容隔开。所以我不知道有什么区别。

这句话很可能是罪魁祸首:

获取卡片。推送(卡片);

看看为什么,考虑这个代码:

让xs=['x','y','z'];
xs.push(['a','b','c']);
//xs现在是:
//[‘x’、‘y’、‘z’、[‘a’、‘b’、‘c’]]
//
// ... 但你可能已经预料到了:
//['x','y','z','a','b','c']
如果我们
join(',')
this,那么
['a','b','c']
元素将转换为字符串:

xs.join(',')/“x,y,z,a,b,c”
请注意缺少空间

每当数组自动转换为字符串时,就会发生此行为:

'xyz'+['a','b','c']/“xyza,b,c”
要修复此问题,请尝试以下更改:

AcquiredCards=AcquiredCards.concat(卡片);

Discord输出有什么问题?他使用了
.join(“,”
),但在输出中没有空格
获得的卡是一个数组
pack
是一个数组。将一个数组推送到一个数组中应该生成一个嵌套数组。因此,执行
pack.join
似乎太高了。我认为您正在实现从数组到字符串的自动转换,其行为类似于
join()
。提示:
'myarray:'+['a','b']
我想你的问题是你需要使用
concat()
而不是
push()
,来将数组添加到包数组中。或者可能是拼接
// 
case "pack":
    Functions.getCards();
    message.channel.send("Cards obtained: " + Functions.pack.join(", "));